在Parse中一次更新多个对象
Update many objects at once in Parse
我需要一次更新一堆对象,而且由于文档建议调用.getObjectInBackgroundWithID
,我找不到一种有效的方法来一次完成所有操作。我没有给每个对象都设置ID,即使我有,我也不能把它们都传递进去。
问题:1)在云代码中调用这个函数比在客户端处理这些更有意义,对吗?2)在JS(云代码)/Swift的for
循环中更新具有相同值的许多对象的最佳方法是什么?
我认为您正在寻找.findObjects
(及其变体)的查询,然后使用PFObject的类方法.saveAll
(及其变体)来保存对象数组。
下面是一个示例:
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!)")
}
}
CloudCode示例:
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);
}
});
相关文章:
- 仅当javascript的新内容出现时,才每3秒更新一次javascript
- 我怎么能有一个实时计时器,在3秒的循环内每秒钟更新一次
- 网络摄像头图片作为背景每60秒更新一次
- 如何每 n 分钟使用数据更新一次 Web 应用程序
- React.js - componentWillReceiveProps只更新一次
- Spotfire Java脚本将字段拆分为唯一值,并每30秒更新一次MapChart可视化,以反映值的变化
- 为什么我的Javascript全局变量只更新一次?jQuery最后一个子选择器错误
- Javascript每15秒更新一次值
- MySQL事件每快照5分钟更新一次
- 将数据传递到另一个php文件以更新数据库的Jquery Ajax只允许更新一次(Wordpress)
- 在“;选择“;只更新一次新选项卡中打开的url
- 创建一个每秒更新一次的计数器
- 一旦套接字发出事件(?),每5秒更新一次组件
- 创建一个每秒钟更新一次的日期和时间函数
- 每10秒更新一次的时间轴
- 每隔5天更新一次
- 多次选择选项更新一次按钮按下
- 每秒钟更新一次React组件
- 使用setInterval()每30秒更新一次时间戳
- 每秒钟更新一次图表