2014-01-26 214 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? –