1

我必须更新 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];

}];

如何更新多条记录?

为了更清楚,我必须执行类似的查询: Update tableName SET messageStatus = 'read' Where userId = 'someId'

有什么帮助吗?

4

2 回答 2

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");
}];
于 2013-09-23T12:45:42.487 回答
0

如果是大量记录,我建议您通过云代码执行更新。您需要做的是查询并接收记录列表,然后遍历列表,更新记录,然后保存记录。

您可以在https://www.parse.com/docs/cloud_code_guide找到有关云代码的信息。

于 2013-08-27T12:39:37.097 回答