简体   繁体   English

iOS Parse.com更新对象

[英]iOS Parse.com updating objects

I am building a photo sharing app using parse as my backend. 我正在建立一个使用parse作为后端的照片共享应用程序。 Now I want to update the numberOfLikes attribute of each photo object after the user clicks the like button, using: 现在,我想在用户单击“赞”按钮后使用以下命令更新每个照片对象的numberOfLikes属性:

[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? (Parse会一一处理吗?)假设这张照片有0个赞,那么在网络条件相同的情况下,有5个用户同时喜欢它,保存后的最终结果是1还是5?

Sorry if this question looks silly or my description is confusing. 抱歉,这个问题看起来很傻,或者我的描述令人困惑。 I am pretty new to Parse. 我对Parse很陌生。 Thanks. 谢谢。

Certain functions in Parse are atomic, incrementKey happens to be one of them. Parse中的某些函数是原子函数,incrementKey恰好是其中之一。

https://parse.com/questions/concurrency-management-in-parse 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. 玩家A调用奖品上的增量密钥方法,并尝试保存(使用回调函数。)玩家B调用奖品上的增量密钥方法,并尝试保存(使用回调函数。)玩家A或玩家B的回调都将运行,numberOfWins为6。剩余的Players回调运行,numberOfWins为7。

You could also implement some sort of locking mechanism using these functions as seen in this question. 您还可以使用这些功能实现某种锁定机制,如本问题所示。

https://parse.com/questions/locking https://parse.com/questions/locking

David's solution is clever and works (incrementKey is atomic). David的解决方案很聪明并且有效(incrementKey是原子的)。 If a GameRequest object is created with a "challengers" : 0 value, then each challenger could call incrementKey:@"challengers". 如果创建的GameRequest对象具有“ challengers”:0值,则每个质询者都可以调用crementKey:@“ challengers”。 If, after save, the value of challengers is 1, then they are the first committed challenger. 保存后,如果挑战者的值是1,则他们是第一个提交的挑战者。 I particularly like this solution since it works for N player games as well. 我特别喜欢这个解决方案,因为它也适用于N个玩家游戏。 Similarly, you can use addUnique: (also atomic) to add a User ID to a list of challengers. 同样,您可以使用addUnique :(也是原子的)将用户ID添加到挑战者列表中。

Finally to answer your question, it should be 5 as they are executed one after another and not concurrently. 最后回答您的问题,应该是5,因为它们是一个接一个地执行,而不是同时执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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