jwBurnside
jwBurnside

Reputation: 867

Pinning a User relationship in Parse

I'm using Parse for storing my cloud data. When I start the app, I have an initializeData() method that fetches the data I'd like to store locally. For instance, I'd like to get and locally save a list of the logged in user's favorite nannys:

    final UzurvUser user = UzurvUser.currentUzurvUser();
    user.getFavoriteNannys().getQuery().findInBackground(new FindCallback<UzurvUser>() {
        @Override
        public void done(final List<UzurvUser> nannys, ParseException e) {
            if (e == null) {
                UzurvUser.unpinAllInBackground(NANNYS_GROUP_LABEL,
                        new DeleteCallback() {
                            public void done(ParseException e) {
                                if (e == null) {
                                    UzurvUser.pinAllInBackground(NANNYS_GROUP_LABEL, nannys);
                                } else {
                                    e.printStackTrace();
                                }
                            }
                        });
            } else {
                e.printStackTrace();
            }
        }
    });

Then, I retrieve them later:

ParseQuery<UzurvUser> query = ParseQuery.getQuery(UzurvUser.class);
query.fromPin(MainActivity.NANNYS_GROUP_LABEL);
mFavoriteNannys = new ArrayList<>(query.find());

The issue is that now if I want to delete a nanny from the user relation and save it in the cloud, I'm going to have to update both the local copy of the Nanny list, and save the current user:

 ParseRelation<UzurvUser> pr = UzurvUser.currentUzurvUser().getFavoriteNannys();
 pr.remove(nannyToRemove);  
 mNanny.unpinInBackground(MainActivity.NANNYS_GROUP_LABEL);
 UzurvUser.currentUzurvUser().saveEventually();

Is this the "correct" way of syncing the data as I have described? I'd like to not have to update both the list of nannys and save the user every time I add or remove a nanny from the user relationship, but I'd still like to have a local copy of all of this user's nannys.

Upvotes: 0

Views: 53

Answers (1)

deinier
deinier

Reputation: 450

If you want to get the same list in both places, your local db and your server, I do think that you should send a request to add/update/delete this item in the server using an AsynTask or other method, and then, when you receive a confirmation that this change was performed in the server, add/update/delete your item in your local db.

Upvotes: 1

Related Questions