之間的區別我有一個雲代碼函數,它將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。更新 –