3

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()
 }
}
0

1 Answer 1

6

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:

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for this. would you be able to help me out with some sample code with the example above
I added a short sample that should work. I wrote it from the top of my head and din't compile it, so it might need some tweaking :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.