SaveAllInBackground根据需要在deleteAllInBackground中不起作用。
我正在尝试使用后台全部保存来保存一个parseobjects列表。为避免表中的重复,我查询已存在的行并删除它们(如果有)然后保存新副本。因此我在deleteAllInBackground的回调中调用saveAllInBackground。
问题在于:
例如:如果要删除的列表包含[a,b,c,d],并且要上传的列表包含[a,b,c,d,e,f],则[e,f]只会被解析为解析。我将[a,b,c,d,e,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();
}
});
}
}
});
}
}
});
答案 0 :(得分:1)
我想出了这样的解决方案。它符合我的要求。我使用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");
}
});
}
}
});
答案 1 :(得分:-1)
是的,如果我理解正确,你只想从localdb保存新数据到解析后端。
最佳和较少请求的解决方案是在您的表中添加一个名为&#34; Draft&#34;或&#34; isUpdated&#34; (你想要的名字)。此标志的作用是识别此字段是否保存在后端。如果它是一个新的领域&#34; isUpdated&#34;是假的,不然是真的。然后在查询中,您只能查询isUpdated是否为false。然后将它们保存在后端。然后
希望这有帮助