2013-08-27 159 views
1

我必須更新parse.com上的表中的多個記錄。它涉及將消息狀態從未讀取更改爲在多個記錄中讀取。 這是更新單個記錄的示例。更新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]; 

}]; 

如何更新多個記錄?

更多的澄清,我必須執行的查詢是這樣的: 更新表名SET messageStatus = '讀' 其中userid = 'someId'

任何幫助嗎?

回答

0

如果它是大量的記錄,我建議你通過雲代碼執行更新。你需要做的是查詢和接收記錄列表,然後遍歷列表,更新記錄,然後保存記錄。

你可以在https://www.parse.com/docs/cloud_code_guide找到關於雲代碼的信息。

+0

此雲代碼是否適用於IOS?其實我想要從ios的代碼更新行。這是可行的嗎? – stackNeverFlow

+0

您可以從ios設備調用雲代碼。 – vlio20

+0

感謝您的幫助,我會檢查您的建議,但我正在尋找查詢或其他方式,我可以一次更新記錄,與我們在SQL查詢中一樣。 – stackNeverFlow

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"); 
}];