Swift Parse Race Conditions

124 Views Asked by At

I am working on an iOS application that will take in ratings from users and come up with an average rating for a specific place based on all those ratings from the users.

No code has been written yet I am still working on the database design.

My approach was going to be have one Class "Restaurants", then in that Class each object would represent a specific restaurant.

Then each restaurant would have three integer fields. "Good", "Bad", and "Neutral" corresponding to the possible ratings that can be given.

Next whenever a user leaves a rating on that restaurant I would update the field and increment it by 1.

I'm curious if two users both leave a "Good" rating on restaurant ABC at the same time which had a value of X for example. Does the Parse API have a way to handle this race condition so the value would end up being X+2 or is it possible that it would be X+1 only.

Does anyone know if Parse handles this by default or what I can do code wise to handle the race condition? I was thinking of setting a field in the object "Locked" to true when a user has downloaded the object and is modifying it and upon saving set the "Locked" field to false so another user can access it. But in the event 100 users are all leaving a rating this will slow down the process of all of them leaving a rating by a lot.

What is the best solution to this?

0

There are 0 best solutions below