在 Before_Save 中解析云代码更新计数器变量

Parse Cloud Code Update Counter Variable in Before_Save

我正在使用 Parse 云代码在某些 class 上更新 beforeSave/afterDelete 中某个用户的一些计数器。用户有一个计数器来跟踪他们的订阅数量。订阅 class 是一个用户与另一个 class 之间的关系 class。用户订阅计数在订阅前保存和删除后更新。

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 并快速创建订阅对象。不幸的是,上面的代码似乎跟不上。似乎如果订阅添加得太快,行 var subscriptionCount = user.get("subscriptionCount"); 在前一个 before_save 保存用户之前被调用,我们最终得到一个过时的订阅计数。

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

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 中发生了同样的事情。知道我该如何解决这个问题吗?我能想到的唯一其他选择是直接查询订阅 class,但这是一种非常低效的方法。

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

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

自增是一个原子操作。