Reputation: 1239
I am building a photo sharing app using parse as my backend. Now I want to update the numberOfLikes attribute of each photo object after the user clicks the like button, using:
[photoObject incrementKey:@"numberOfLikes"];
[photoObject saveInBackground];
My question is, lets say several users liked the same photo at the same time, will these like requests be in a processing queue? (Will Parse handle them one by one?)Assuming this photo has 0 likes, then 5 users like it at the same time, given that the network conditions are the same, will the final result after saving be 1 or 5?
Sorry if this question looks silly or my description is confusing. I am pretty new to Parse. Thanks.
Upvotes: 1
Views: 115
Reputation: 3468
Certain functions in Parse are atomic, incrementKey happens to be one of them.
https://parse.com/questions/concurrency-management-in-parse
The incrementKey method is atomic when saving. So the process goes like this:
Player A calls the incrementKey method on the Prize, and attempts to save (with a callback function.) Player B calls the incrementKey method on the Prize, and attempts to save (with a callback function.) Either Player A or Player B's callback runs, the numberOfWins is 6. The remaining Players callback runs, the numberOfWins is 7.
You could also implement some sort of locking mechanism using these functions as seen in this question.
https://parse.com/questions/locking
David's solution is clever and works (incrementKey is atomic). If a GameRequest object is created with a "challengers" : 0 value, then each challenger could call incrementKey:@"challengers". If, after save, the value of challengers is 1, then they are the first committed challenger. I particularly like this solution since it works for N player games as well. Similarly, you can use addUnique: (also atomic) to add a User ID to a list of challengers.
Finally to answer your question, it should be 5 as they are executed one after another and not concurrently.
Upvotes: 1