iOS Parse.com更新对象 [英] iOS Parse.com updating objects

查看:82
本文介绍了iOS Parse.com更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用parse作为后端构建照片共享应用程序.现在,我想在用户单击赞"按钮后使用以下方法更新每个照片对象的numberOfLikes属性:

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];

我的问题是,假设有多个用户同时喜欢同一张照片,这些喜欢的请求是否会在处理队列中? (解析将一一处理吗?)假设这张照片有0个赞,那么在网络条件相同的情况下,有5个用户同时喜欢它,保存后的最终结果是1还是5?

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的新手.谢谢.

Sorry if this question looks silly or my description is confusing. I am pretty new to Parse. Thanks.

推荐答案

Parse中的某些函数是原子函数,incrementKey恰好是其中之一.

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:

玩家A调用奖品上的增量密钥方法,并尝试保存(使用回调函数). 玩家B调用奖品上的增量密钥方法,并尝试保存(使用回调函数.) 运行播放器A或播放器B的回调,numberOfWins为6. 运行剩余的Players回调,numberOfWins为7.

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的解决方案很聪明并且可以工作(incrementKey是原子的).如果创建的GameRequest对象具有"challengers":0值,则每个质询者都可以调用crementKey:@"challengers".保存后,如果挑战者的值是1,则他们是第一个提交的挑战者.我特别喜欢这个解决方案,因为它也适用于N个玩家游戏.同样,您可以使用addUnique:(也是原子的)将用户ID添加到挑战者列表中.

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.

最后回答您的问题,因为它们是一个接一个地执行,而不是同时执行,所以应该为5.

Finally to answer your question, it should be 5 as they are executed one after another and not concurrently.

这篇关于iOS Parse.com更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆