SaveAllInBackground根据需要在deleteAllInBackground中不起作用

555*_*597 7 android parse-platform

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钩子.调用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();
                                        }
                                    });
                                }
                            }
                        });


                    }
                }
            });
Run Code Online (Sandbox Code Playgroud)

555*_*597 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");
        }
    });

                    }
                }
            });
Run Code Online (Sandbox Code Playgroud)