Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update multiple objects with Parse?

How can you update multiple objects to Parse in a single query?

The code below is for a single entry.

How can I go about updating multiple values, which I get from an array?

 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()
 }
}
like image 374
Mugs Avatar asked Sep 28 '26 17:09

Mugs


1 Answers

You can use the saveAll*: methods to save a batch of objects at once without calling save on each of them individually. Basically, you put all the objects you want to save into an array and then call [PFObject saveAll:yourArray] (just an example you can use saveAllInBackground: as well).

Here is some sample code in Objective-C, I am not very good with Swift but you probably can translate it easily.

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:

like image 177
Björn Kaiser Avatar answered Sep 30 '26 11:09

Björn Kaiser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!