Adam First
Adam First

Reputation: 445

Parse: +1 to a number column for rating

My Android app has a very simple love it or hate it rating system. I am using Parse for the ratings.

All is fine in terms of querying Parse for number of ratings and also putting new ratings into parse.

The problem I'm having is:

I have not found a way on Parse to +1 to a number column, so what I have been doing is getting the current value of the rating, +1 to that number and then using Parse's Put method to write it back.

This seems ok but if 2 users open the app 1 second after the other they both see the rating is 8, then both click +1, it will get the value of the rating (8) for user A, +1 to 8 = 9 and then write 9 back.

1 second later user B does the same thing, they still have the value of 8 stored on their device so theirs will also be 8 +1 = 9, where it should have been 10.

So my question is: Is there a way to +1 to a number column in Parse?

If not I'll add the getRatings method before a user rates so it should get the latest value but was just hoping to reduce bandwidth.

Thanks.

Adam

public void getRatings() {
    //query to get love it/hate it from Parse
    //we are only quering to get the first in background because we know there is only
    //one we want
    ParseQuery<ParseObject> query = ParseQuery.getQuery("ratings");
    query.whereEqualTo("Id", id);
    query.getFirstInBackground(new GetCallback<ParseObject>() {
        @Override
        public void done(ParseObject object, ParseException error) {
            if (object != null) {
                //if the show has been rated before
                loveIt = object.getInt("loveIt");
                hateIt = object.getInt("hateIt");
                retreivedObject = object;
                parseId = object.getObjectId();
                //hide progress bar
                mProgressBar.setVisibility(View.INVISIBLE);
            }
            else
            {
                //if the show has not been rated before
                loveIt=0;
                hateIt=0;
                //hide progress bar
                mProgressBar.setVisibility(View.INVISIBLE);
            }
        }
    });
}

Upvotes: 0

Views: 398

Answers (1)

chrissr
chrissr

Reputation: 9921

You need to use a counter. See official documentation below.

https://www.parse.com/docs/android_guide#objects-updating

Upvotes: 3

Related Questions