我必须更新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'
任何帮助?
答案 0 :(得分:3)
首先你应该查询这些对象:
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");
}];
答案 1 :(得分:0)
如果是大量记录,我建议您通过云代码执行更新。您需要做的是查询和接收记录列表,然后遍历列表,更新记录,然后保存记录。
您可以在https://www.parse.com/docs/cloud_code_guide找到有关云代码的信息。