Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SaveAllInBackground doesn't work inside deleteAllInBackground as desired

SaveAllInBackground doesn't work inside deleteAllInBackground as desired.

I am trying to save a list of parseobjects using save all in background. To avoid duplicates in the table, I am querying for the already existing rows and deleting them if any and then save the new copy. Therefore I am calling the saveAllInBackground inside the deleteAllInBackground's callback.

The problem is this :

For ex: if the list to delete contains [a,b,c,d] and the list to upload has [a,b,c,d,e,f] only [e,f] get persised to parse. I am passing [a,b,c,d,e,f] to the saveAllInBackground but only [e,f] get persisted.

  • Is there something I am missing? How to solve this?

  • Can I use a different approach?

  • Is there a better way to avoid duplicates? I dont want to add a beforeSave hook. The whole purpose of calling the saveAll is to reduce the number of API calls. I guess if I use beforeSave, I will have to run some queries in the cloud code anyway.

This is my code

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


                    }
                }
            });
like image 854
55597 Avatar asked Jun 23 '15 18:06

55597


1 Answers

I have come up with a solution like this. and it meets my requirement. I use the updatedValue and delete the old ones and the remaining get updated as a whole list.

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

                    }
                }
            });
like image 72
55597 Avatar answered Oct 14 '22 08:10

55597