我必须更新parse.com上一个表中的多个记录。它与将消息状态从未读更改为在多个记录中读取有关。
这是更新单个记录的示例。
PFQuery *query = [PFQuery queryWithClassName:@"GameScore"];
// Retrieve the object by id
[query getObjectInBackgroundWithId:@"xWMyZ4YEGZ" block:^(PFObject *gameScore, NSError *error) {
// Now let's update it with some new data. In this case, only cheatMode and score
// will get sent to the cloud. playerName hasn't changed.
[gameScore setObject:[NSNumber numberWithBool:YES] forKey:@"cheatMode"];
[gameScore setObject:[NSNumber numberWithInt:1338] forKey:@"score"];
[gameScore saveInBackground];
}];
如何更新多个记录?
为了更清楚,我必须执行类似以下的查询:
更新tableName SET messageStatus ='read'其中userId ='someId'
有什么帮助吗?
最佳答案
首先,您应该查询以下对象:
NSArray *arrayOfMessageIDs = @[@"2314312", @"312432423"]; // An array of your desired messages' ID
PFQuery *query = [PFQuery queryWithClassName:@"Message"];
[query whereKey:@"messageID" containedIn:arrayOfMessageIDs];
然后,您遍历它们并更新它们的状态。
之后,保存所有阵列。
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
if (error) {
NSLog(@"%@", error);
return;
}
for (PFObject *message in objects) {
// Update your message
}
int success = [PFObject saveAll:objects];
NSLog(@"Status %@", success? @"updated successfully": @"update failed");
}];
关于iphone - 在1个查询调用中更新多行。 Parse.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18459243/