之间的区别我有一个云代码函数,它将Notification对象标记为“read”,然后立即查询同一个类,以便保留“未读”对象。Parse.Object.saveAll(objects)和Parse.Promise.when(promiseListOfSaves)
问题是大多数次,即使数据浏览器中的值已更新,我标记为已读的通知对象也会返回到我先前的未读查询中。
当我从Parse.Promise.when()
更改为Parse.Object.saveAll()
时,解决了这个问题,但它仍然不清楚原因。当所有的save()
操作完成时应该解决何时应答,因此在被标记为已读之前不会运行最后一个“未读”查询。
Parse.Object.saveAll(objects)
这样做Parse.Promise.when(promiseListOfSaves)
不?
以下每个代码示例。除了物体被保存的方式之外,两者之间没有太大的区别。
Parse.Cloud.define('markThreadReadAndReturnUnreadCount', function(request, response){
var promiseChain = [];
var Notification = Parse.Object.extend("Notification");
qry = new Parse.Query(Notification);
qry.equalTo('owner', request.user);
qry.equalTo('read', false);
qry.equalTo('messageThreadId', request.params.threadId);
qry.find(null, {useMasterKey: true}).then(function(_notifications){
_.each(_notifications, function(_notification){
notification = new Notification();
notification.id = _notification.id;
notification.set('read', true);
promiseChain.push(notification.save(null, {wait: true}));
});
return Parse.Promise.when(promiseChain);
}).then(function(){
var countUnreadQuery = new Parse.Query(Notification);
countUnreadQuery.equalTo('owner', user);
countUnreadQuery.equalTo('read', false);
countUnreadQuery.find({useMasterKey: true}).then(function(results){
return response.success(results.length);
}
};
});
with Parse.Object.saveAll();
Parse.Cloud.define('markThreadReadAndReturnUnreadCount', function(request, response){
var saveObjects = [];
var Notification = Parse.Object.extend("Notification");
qry = new Parse.Query(Notification);
qry.equalTo('owner', request.user);
qry.equalTo('read', false);
qry.equalTo('messageThreadId', request.params.threadId);
qry.find(null, {useMasterKey: true}).then(function(_notifications){
_.each(_notifications, function(_notification){
notification = new Notification();
notification.id = _notification.id;
notification.set('read', true);
saveObjects.push(notification);
});
return Parse.Object.saveAll(saveObjects);
}).then(function(){
var countUnreadQuery = new Parse.Query(Notification);
countUnreadQuery.equalTo('owner', user);
countUnreadQuery.equalTo('read', false);
countUnreadQuery.find({useMasterKey: true}).then(function(results){
return response.success(results.length);
}
};
});
我做了一些副本编辑大多只是打破了一个长款分成多个段落更好的可读性,在尝试的精神确保你在这里得到一个很好的答案。 (顺便说一下,欢迎使用StackOverflow。) – sideshowbarker
@Tariq,我想你需要将第二块代码更新为Object.saveAll – mido
谢谢@ mido22。更新 –