我正在使用Parse云代码在某些类上的beforeSave / afterDelete中更新用户的一些计数器。用户具有一个可跟踪其订阅数量的计数器。订阅类是用户和另一个类之间的关系类。用户订阅计数将在订阅的beforeSave和afterDelete中更新。

Parse.Cloud.beforeSave("Subscription", function(request, response) {
    var fromUserPointer = subscription.get("fromUser");

    fromUserPointer.fetch().then(function(user){

        var subscriptionCount = user.get("subscriptionCount");

        console.log( subscriptionCount + 1);
        user.set("subscriptionCount", subscriptionCount + 1);
        return user.save();
    });
});


在客户端iPhone应用程序上,用户可以浏览tableView并快速创建订阅对象。不幸的是,上述代码似乎无法跟上。看来,如果订阅添加得太快,则会在先前的before_save保存用户之前调用var subscriptionCount = user.get("subscriptionCount");行,并且最终导致陈旧的订阅计数。

下面是控制台输出的一个示例,演示了这一点:

I2015-02-17T23:10:14.972Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: {...}
I2015-02-17T23:10:15.022Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: {...}
I2015-02-17T23:10:15.073Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: {...}
I2015-02-17T23:10:15.145Z] 1
I2015-02-17T23:10:15.224Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":1}}
  Result: Update changed to {"followersCount":0,"followingCount":0,"subscriptionCount":1}
I2015-02-17T23:10:15.244Z] 1
I2015-02-17T23:10:15.257Z] 1
I2015-02-17T23:10:15.276Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":1}}
  Result: Update changed to {"followingCount":0,"subscriptionCount":1,"followersCount":0}
I2015-02-17T23:10:15.329Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":1}}
  Result: Update changed to {"subscriptionCount":1,"followersCount":0,"followingCount":0}
I2015-02-17T23:10:15.576Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: {...}
I2015-02-17T23:10:15.611Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: ...
I2015-02-17T23:10:15.685Z] v268: before_save triggered for Subscription for user NHl80OFkKm
  Input: {...}
  Result: ...
I2015-02-17T23:10:15.692Z] 2
I2015-02-17T23:10:15.706Z] 2
I2015-02-17T23:10:15.715Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":2}}
  Result: Update changed to {"subscriptionCount":2,"followersCount":0,"followingCount":0}
I2015-02-17T23:10:15.732Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":2}}
  Result: Update changed to {"followersCount":0,"followingCount":0,"subscriptionCount":2}
I2015-02-17T23:10:15.789Z] 3
I2015-02-17T23:10:15.817Z] v268: before_save triggered for _User for user NHl80OFkKm
  Input: { ... "update":{"subscriptionCount":3}}
  Result: Update changed to {"followersCount":0,"followingCount":0,"subscriptionCount":3}


在afterDelete中也发生了同样的事情。知道如何解决这个问题吗?我可以想到的唯一其他选择是对订阅类进行直接查询,但这是一种非常低效的方法。

最佳答案

我应该仔细阅读一下文档。此用例在此处记录:https://parse.com/docs/js_guide#objects-updating

只需将user.set("subscriptionCount", subscriptionCount + 1);替换为user.increment("subscriptionCount", 1);

增量是原子操作。

10-07 21:18