Javed Iqbal
Javed Iqbal

Reputation: 453

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;

Upvotes: 5

Views: 13071

Answers (3)

user2849654
user2849654

Reputation:

Just save the objectId NSString.

Then to retreive, do some fancy stuff like this.

[query2 whereKey:PF_CHAT_SETID equalTo:[PFObject objectWithoutDataWithClassName:PF_SET_CLASS_NAME objectId:setId_]];

Upvotes: 0

arangelp
arangelp

Reputation: 141

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];

Upvotes: 9

lxt
lxt

Reputation: 31304

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.

Upvotes: 5

Related Questions