简体   繁体   中英

Parse.com saveAllInBackground doesn't work inside deleteAllInBackground

I am trying to save a list of parseobjects using save all in background. To avoid duplices in the table, I am querying for the already existing rows and deleting them if any and save the new copy.

The ParseObject.saveAllInBackground gets execued, the callbacks are getting called, but no rows are getting saved.

I am sure the list I am passing to the saveAllInBackground has parseObjects.

I debugged the methods, the flow runs as intended.

Update 1:

When the number of rows to delete is less than the number of rows added, the newer rows get persisted. Meaning, the rows that are not present in the list passed to deleteallinbackground method get persisted.

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() {

//This method works fine and uploads. The issue arises only when saveAllInBackground is called inside deleteAllInBackground's call back

                                            @Override
                                            public void done(ParseException e) {
                                                Log.i("SyncChoiceService", "Synced Choices");
                                                editor.putLong(Four.LAST_CHOICE_SYNC_TIME,System.currentTimeMillis());
                                                editor.commit();
                                            }
                                        });
                                    }
                                }
                            });


                        }
                    }
                });
  • query.fromPin(); - this might be using your local datastore, not sure
  • ParseObject.saveAllInBackground(localList... make sure that localList is not empty. I have a suspicion it is when you call the save method.
  • First two queries dublicate themselves, just make this query instead of those two:

     ParseQuery query = new ParseQuery("PostChoice"); query.whereEqualTo("user",ParseUser.getCurrentUser()); 
  • im not sure, but there might be a limit to nesting bacground callbacks. You might try to call a method in your activity class from one of the callbacks, that continues the chain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM