Prevent duplicate entries parse.com

I'm using Parse.com as my backend and while there seems to be a method, saveInBackgroundWithBlock , to prevent duplicate entries. It doesn't appear to exist on Android. I'd like to upload only unique entries but can't figure out a way to do so.

The only thing I can think of is to query then insert if the entry doesn't exist, but that's doing twice as many network calls and I feel like it needs to.

Thanks


As I had mentioned in the comment earlier, I had faced the same problem. Ended up writing a query to find the existing objects and then save only the non-existing ones. Like below.

//Say you have a list of ParseObjects..this list contains the existing as well as the new objects.

List<ParseObject> allObjects = new ArrayList<ParseObject>();
allObjects.add(object); //this contains the entire list of objects.

You want to find out the existing ones by using the field say ids.

//First, form a query
ParseQuery<ParseObject> query = ParseQuery.getQuery("Class");
query.whereContainedIn("ids", allIds); //allIds is the list of ids

List<ParseObject> Objects = query.find();  //get the list of the parseobjects..findInBackground(Callback) whichever is suitable

for (int i = 0; i < Objects.size(); i++)
      existingIds.add(Objects.get(i).getString("ids"));

List<String> idsNotPresent = new ArrayList<String>(allIds);
idsNotPresent.removeAll(existingIds);

//Use a list of Array objects to store the non-existing objects
List<ParseObject> newObjects = new ArrayList<ParseObject>();

for (int i = 0; i < selectedFriends.size(); i++) {
     if (idsNotPresent.contains(allObjects.get(i).getString(
                        "ids"))) {
     newObjects.add(allObjects.get(i)); //new Objects will contain the list of only the ParseObjects which are new and are not existing.
    }
}

//Then use saveAllInBackground to store this objects

ParseObject.saveAllInBackground(newObjects, new SaveCallback() {

    @Override
    public void done(ParseException e) {
    // TODO Auto-generated method stub
    //do something
        }
    });

I had also tried using beforeSave method on ParseCloud . As you may know, before saving the objects this method is called on the ParseCloud and is ideal to make any validation required. But, it didn't quite run well. Let me know if you need something from the ParseCloud code.

Hope this helps!


我不确定我是否理解你的问题,但是你可以在Android中获得与saveInBackgroundWithBlock相同的功能,如下所示:

myObject.saveInBackground(new SaveCallback() {
    public void done(ParseException e) {
        if (e == null) {
           myObjectSavedSuccessfully();
        } else {
           myObjectSaveDidNotSucceed();
        }
    }
});
链接地址: http://www.djcxy.com/p/18794.html

上一篇: 尽管使用了随机种子,随机数仍然保持不变

下一篇: 防止重复条目parse.com