Search code examples
ios7parse-platform

Save data from NSArray to Parse


I have array of NSObjects. Each element have the following properties (name, id, comment). I'm using parse.com server to send and retrieve my iOS app data. My code for saving data is the following

- (IBAction)order:(id)sender {

    PFObject *obj = [PFObject objectWithClassName:@"Table_1"];

    for (SelectedIteam *iteam in _dataArray) {
        [obj setObject:iteam.name forKey:@"Name"];
        [obj setObject:iteam.id forKey:@"ID"];
        [obj setObject:iteam.comment forKey:@"Comment"];
        [obj saveInBackground];
   }

}

But only the last element of my nsarray is saved in parse server. How can i save all elements from array to parse server.


Solution

  • I would use PFObject's saveAllInBackground method instead. Otherwise you are making one api call to Parse for every object in the array when you can achieve the same with just one call.

    - (IBAction)order:(id)sender {
    
         NSMutableArray *items = [[NSMutableArray alloc] init];
    
         for (SelectedIteam *iteam in _dataArray) {
    
              PFObject *obj = [[PFObject objectWithClassName:@"Table_1"];
    
              [obj setObject:iteam.name forKey:@"Name"];
              [obj setObject:iteam.id forKey:@"ID"];
              [obj setObject:iteam.comment forKey:@"Comment"];
    
              [items addObject: obj];
          }
    
          [PFObject saveAllInBackground:items];
    }
    

    https://parse.com/docs/ios/api/Classes/PFObject.html#//api/name/saveAllInBackground: