如何在单个查询中将多个对象更新为Parse?
以下代码适用于单个条目。
如何更新我从数组中获取的多个值?
var query = PFQuery(className:"GameScore")
query.getObjectInBackgroundWithId("xWMyZEGZ") {
(gameScore: PFObject?, error: NSError?) -> Void in
if error != nil {
println(error)
} else if let gameScore = gameScore {
gameScore["cheatMode"] = true
gameScore["score"] = 1338
gameScore.saveInBackground()
}
}
答案 0 :(得分:5)
您可以使用saveAll*:
方法一次保存一批对象,而无需单独调用每个对象的保存。基本上,您将要保存的所有对象放入一个数组中,然后调用[PFObject saveAll:yourArray]
(只是一个示例,您也可以使用saveAllInBackground:
)。
以下是Objective-C中的一些示例代码,我对Swift不太满意,但您可能很容易翻译它。
PFQuery *query = [PFQuery queryWithClassName:@"GameScore"];
[query findInBackgroundWithBlock:^(NSArray *results, NSError *error) {
NSMutableArray *saveAllOfMe = [NSMutableArray new];
for (PFObject *object in results) {
object[@"cheatMode"] = @(YES);
object[@"score"] = @(1234);
[saveAllOfMe addObject:object];
}
[PFObject saveAllInBackground:saveAllOfMe block:^(BOOL success, NSError *error) {
// Check result of the operation, all objects should have been saved by now
}];
}];
https://www.parse.com/docs/ios/api/Classes/PFObject.html#//api/name/saveAllInBackground:block: