gpt4 book ai didi

android - SaveAllInBackground 不能根据需要在 deleteAllInBackground 内部工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:15:36 25 4
gpt4 key购买 nike

SaveAllInBackground 在 deleteAllInBackground 中无法正常工作。

我正在尝试在后台使用全部保存来保存解析对象列表。为了避免表中的重复项,我正在查询已经存在的行并删除它们(如果有)然后保存新副本。因此,我在 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();
}
});
}
}
});


}
}
});

最佳答案

我想出了这样的解决方案。它符合我的要求。我使用 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");
}
});

}
}
});

关于android - SaveAllInBackground 不能根据需要在 deleteAllInBackground 内部工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31010474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com