2014-01-26 225 views
7

我使用Parse.com作为我的后端,而似乎有一种方法,saveInBackgroundWithBlock,以防止重复条目。它似乎并不存在于Android上。我只想上传唯一的条目,但无法找到一种方法。防止重复条目parse.com

我能想到的唯一的事情是查询,然后插入如果条目不存在,但这是做了两次网络调用,我觉得它需要。

感谢

+0

面临同样的问题。结束编写查询来查找现有对象,然后仅保存不存在的对象。 – droidx

+0

你有任何解决方案? –

回答

0

我不知道我理解你的问题,但你可以在Android中像这样得到相同的功能saveInBackgroundWithBlock

myObject.saveInBackground(new SaveCallback() { 
    public void done(ParseException e) { 
     if (e == null) { 
      myObjectSavedSuccessfully(); 
     } else { 
      myObjectSaveDidNotSucceed(); 
     } 
    } 
}); 
+0

我想避免保存重复的条目。 – snotyak

+0

如果您多次调用对象保存,则只能保存重复条目。请在您的问题中提供一些背景信息,以便我们了解为什么您必须对同一个对象执行多次保存调用。 – bobbyrehm

3

正如我曾在评论前面提到的,我面临同样的问题。结束编写查询来查找现有对象,然后仅保存不存在的对象。如下所示。

//假设你有一个ParseObjects ...的列表,这个列表包含现有的以及新的对象。

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

你想通过使用字段说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 
     } 
    }); 

我也曾尝试在ParseCloud上使用beforeSave方法。如您所知,在保存对象之前,此方法在ParseCloud上调用,并且非常适合进行任何验证。但是,它运行得并不顺利。让我知道你是否需要ParseCloud的代码。

希望这会有所帮助!

+1

@BackpackOnHead做了这个帮助吗?你有没有找到更好的方法来做到这一点?请让我知道。谢谢! – droidx

+0

可以请你回答这个类似的问题http://stackoverflow.com/questions/25801533/what-is-the-best-way-to-get-distinct-contacts-in-android? –