Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save array of objects using parse.com server iOS?

Iam using parse.com server to send and retrieve my iOS app data. I want to save list of songs and each song have the following properties(title, artist, album). My code snippet is here;

        -(IBAction)saveSongsData:(id)sender {

        PFObject *newPlayer = [PFObject objectWithClassName:@"Players"];

        /*[newPlayer addObjectsFromArray:self.songsArrray forKey:@"songs"];
        here i got the exception, if i uncomment it*/

        [newPlayer setObject:self.txtPlayerName.text forKey:@"playerName"];
        [newPlayer setObject:self.txtPlayerDesc.text forKey:@"playerDescription"];
        [newPlayer setObject:self.txtPlayerPass.text forKey:@"playerPassword"];

        NSString *objectId = [newPlayer objectId];
        [[NSUserDefaults standardUserDefaults]setObject:objectId forKey:@"id"];

        [newPlayer saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!error) {
            }
            else {
            }
        }];
        }

Where self.songsArray is an array of songs objects having following properties;

  • title, artist, album.

    But when i try to save my songs data by using this line of code.

        [newPlayer addObjectsFromArray:self.songsArrray forKey:@"songs"];
    

    I got an exception and this is the message:-

    Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'PFObject values must be serializable to JSON'

    Please help me, how to send my songs data to server in JSON format. Thanks.

like image 394
Javed Iqbal Avatar asked Apr 29 '13 14:04

Javed Iqbal


2 Answers

You could try one these options:

1) If you want to save an array of Parse Objects you can use the following:

[PFObject saveAllInBackground: self.songsArray block: YOUR_BLOCK];

2) You can create Parse.com relations:

PFObject *newPlayer ...
PFRelation *relation = [newPlayer relationforKey:@"songs"];
[relation addObject:song];  // add as many songs as you want.
[newPlayer saveInBackground];
like image 103
arangelp Avatar answered Nov 06 '22 19:11

arangelp


When working with Parse, your arrays and dictionaries can only contain objects that can be serialized to JSON. It looks as if self.songsArray contains objects that cannot be automatically converted to JSON. You have two options - use objects that are compatible with Parse or, as someone has suggested in the comments, make your songs PFObjects and then associate them with the player via a relationship.

like image 5
lxt Avatar answered Nov 06 '22 21:11

lxt