SaveAllInBackground根据需要在deleteAllInBackground中不起作用.
我正在尝试使用后台全部保存来保存一个parSEObjects列表.为了避免表中的重复,我正在查询已存在的行并删除它们(如果有)然后保存新副本.因此我在deleteAllInBackground的回调中调用saveAllInBackground.
问题是这样的:
例如:如果要删除的列表包含[a,b,c,d],并且要上传的列表包含[a,d,e,f],则只有[e,f]才能进行解析.我将[a,f]传递给saveAllInBackground但只保留了[e,f].
>有什么我想念的吗?怎么解决这个?
>我可以使用不同的方法吗?
>有没有更好的方法来避免重复?我不想添加一个
beforeSave hook.调用saveAll的全部目的是减少API调用的数量.我想如果我使用beforeSave,我将不得不在云代码中运行一些查询.
这是我的代码
ParseQuery query = new ParseQuery("PostChoice"); query.fromPin(); query.findInBackground(new FindCallback<ParSEObject>() { @Override public void done(final List<ParSEObject> localList,ParseException e) { if (localList != null && !localList.isEmpty()) { List<ParSEObject> postList = new ArrayList<ParSEObject>(); for (ParSEObject object : localList) { postList.add(object.getParSEObject("post")); } ParseQuery query = new ParseQuery("PostChoice"); query.whereContainedIn("post",postList); query.whereEqualTo("user",ParseUser.getCurrentUser()); query.findInBackground(new FindCallback<ParSEObject>() { @Override public void done(List<ParSEObject> parseCloudList,ParseException e) { if (parseCloudList != null && !parseCloudList.isEmpty()) { ParSEObject.deleteAllInBackground(parseCloudList,new DeleteCallback() { @Override public void done(ParseException e) { // this gets executed and rows are accordingly deleted ParSEObject.saveAllInBackground(localList,new SaveCallback() { @Override public void done(ParseException e) { // this gets executed but the rows are not uploaded. //the locallist is not empty. it contains the right data. editor.putLong(Four.LAST_CHOICE_SYNC_TIME,System.currentTimeMillis()); editor.commit(); Log.i("SyncChoiceService","Synced Choices"); } }); } }); } else{ ParSEObject.saveAllInBackground(localList,new SaveCallback() { @Override public void done(ParseException e) { Log.i("SyncChoiceService","Synced Choices"); editor.putLong(Four.LAST_CHOICE_SYNC_TIME,System.currentTimeMillis()); editor.commit(); } }); } } }); } } });
解决方法
我想出了这样的解决方案.它符合我的要求.我使用updatedValue并删除旧的,其余的更新作为整个列表.
ParseQuery query = new ParseQuery("PostChoice"); query.fromPin(); query.findInBackground(new FindCallback<ParSEObject>() { @Override public void done(final List<ParSEObject> localList,ParseException e) { if (localList != null && !localList.isEmpty()) { List<ParSEObject> postList = new ArrayList<ParSEObject>(); for (ParSEObject object : localList) { postList.add(object.getParSEObject("post")); } ParseQuery query = new ParseQuery("PostChoice"); query.whereContainedIn("post",postList); query.whereLessThan("updatedAt",System.currentTimeMillis()); query.whereEqualTo("user",ParseUser.getCurrentUser()); query.findInBackground(new FindCallback<ParSEObject>() { @Override public void done(final List<ParSEObject> parseCloudList,ParseException e) { if (parseCloudList != null && !parseCloudList.isEmpty()) { ParSEObject.deleteAllInBackground(parseCloudList,new DeleteCallback() { @Override public void done(ParseException e) { Log.i("SyncChoiceService","Deleted old Choices"); } }); } } }); ParSEObject.saveAllInBackground(localList,new SaveCallback() { @Override public void done(ParseException e) { Log.i("SyncChoiceService","Synced Choices"); } }); } } });