5

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.

5
  • Don't you need to save the array? [newPlayer save]; I think is how you do it with parse. Commented Apr 29, 2013 at 14:39
  • If i have for example, 7 songs in self.songsArray. Then how to serialize that data in JSON format and save that data using PFObject?OR is there another possibility? Commented Apr 29, 2013 at 14:46
  • Have you checked out their REST API? parse.com/docs/rest#objects-classes Commented Apr 29, 2013 at 14:54
  • 1
    You should probably make the songs objects actual PFObjects, which are part of the parse DB. Then associated the songs with the player (either by ID or relation). Commented Apr 29, 2013 at 15:10
  • Thanks @STANGMMX, It helped to sort out my problem. Commented Apr 29, 2013 at 15:21

3 Answers 3

9

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];
Sign up to request clarification or add additional context in comments.

Comments

5

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.

1 Comment

I am doing like this and its working fine now:) PFObject *songObject = [PFObject objectWithClassName:@"Songs"]; [songObject setObject:songs.title forKey:@"title"]; [songObject setObject:songs.album forKey:@"album"]; [songObject setObject:songs.artist forKey:@"artist"]; [songObject save]; Thanks.
0

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

Comments

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.