在 Parse 中一次更新多个对象
Update many objects at once in Parse
我需要一次更新一堆对象,但由于文档建议调用 .getObjectInBackgroundWithID
,所以我找不到一种有效的方法来一次完成所有这些操作。我没有每个对象的 ID,即使我有我也无法将它们全部传递进去。
问题:
1) 在 Cloud Code 中调用这个函数比在客户端处理这一切更有意义,对吧?
2) 在 JS (Cloud Code)/Swift/Swift 的 for
循环中更新许多具有相同值的对象的最佳方法是什么?
我认为您正在寻找带有 .findObjects
(及其变体)的查询,然后使用 PFObject 的 class 方法 .saveAll
(及其变体)来保存 对象数组.
这是一个sample:
var query = PFQuery(className:"GameScore")
query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
// Do your manipulation
}
// Save your changes to ALL objects
PFObject.saveAllInBackground(objects, block: {
(succeeded: Bool, error: NSError!) -> Void in
if (error == nil) {
println("Successfully saved \(objects!.count) objects.")
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
})
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
query.equalTo("playerName", "Dan Stemkoski");
query.find({
success: function(results) {
alert("Successfully retrieved " + results.length + " scores.");
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
alert(object.id + ' - ' + object.get('playerName'));
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
我需要一次更新一堆对象,但由于文档建议调用 .getObjectInBackgroundWithID
,所以我找不到一种有效的方法来一次完成所有这些操作。我没有每个对象的 ID,即使我有我也无法将它们全部传递进去。
问题:
1) 在 Cloud Code 中调用这个函数比在客户端处理这一切更有意义,对吧?
2) 在 JS (Cloud Code)/Swift/Swift 的 for
循环中更新许多具有相同值的对象的最佳方法是什么?
我认为您正在寻找带有 .findObjects
(及其变体)的查询,然后使用 PFObject 的 class 方法 .saveAll
(及其变体)来保存 对象数组.
这是一个sample:
var query = PFQuery(className:"GameScore")
query.whereKey("playerName", equalTo:"Sean Plott")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
println(object.objectId)
// Do your manipulation
}
// Save your changes to ALL objects
PFObject.saveAllInBackground(objects, block: {
(succeeded: Bool, error: NSError!) -> Void in
if (error == nil) {
println("Successfully saved \(objects!.count) objects.")
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
})
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
query.equalTo("playerName", "Dan Stemkoski");
query.find({
success: function(results) {
alert("Successfully retrieved " + results.length + " scores.");
// Do something with the returned Parse.Object values
for (var i = 0; i < results.length; i++) {
var object = results[i];
alert(object.id + ' - ' + object.get('playerName'));
}
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});