微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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");
        }
    });

                    }
                }
            });

原文地址:https://www.jb51.cc/android/315070.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐