如何使用parse.com server iOS保存对象数组?

时间:2023-01-12 13:20:34

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;

我使用parse.com服务器发送和检索我的iOS应用数据。我想保存歌曲列表,每首歌都有以下属性(标题,艺术家,专辑)。我的代码片段在这里;

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

自我的地方。songsArray是一组具有以下属性的歌曲对象;

  • 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'

    由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因是:“PFObject值必须可序列化为JSON”

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

    请帮助我,如何将我的歌曲数据以JSON格式发送到服务器。谢谢。

3 个解决方案

#1


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.

使用解析时,数组和字典只能包含可序列化为JSON的对象。看起来好像是self。songsArray包含不能自动转换为JSON的对象。您有两个选项——使用与Parse兼容的对象,或者像某些人在评论中建议的那样,让歌曲PFObjects,然后通过关系将它们与播放器关联起来。

#2


9  

You could try one these options:

你可以试试以下方法:

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

1)如果要保存一个解析对象数组,可以使用以下方法:

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

2) You can create Parse.com relations:

2)您可以创建Parse.com关系:

PFObject *newPlayer ...
PFRelation *relation = [newPlayer relationforKey:@"songs"];
[relation addObject:song];  // add as many songs as you want.
[newPlayer saveInBackground];

#3


0  

Just save the objectId NSString.

保存objective NSString。

Then to retreive, do some fancy stuff like this.

然后回去,做一些像这样的奇特的事情。

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

#1


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.

使用解析时,数组和字典只能包含可序列化为JSON的对象。看起来好像是self。songsArray包含不能自动转换为JSON的对象。您有两个选项——使用与Parse兼容的对象,或者像某些人在评论中建议的那样,让歌曲PFObjects,然后通过关系将它们与播放器关联起来。

#2


9  

You could try one these options:

你可以试试以下方法:

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

1)如果要保存一个解析对象数组,可以使用以下方法:

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

2) You can create Parse.com relations:

2)您可以创建Parse.com关系:

PFObject *newPlayer ...
PFRelation *relation = [newPlayer relationforKey:@"songs"];
[relation addObject:song];  // add as many songs as you want.
[newPlayer saveInBackground];

#3


0  

Just save the objectId NSString.

保存objective NSString。

Then to retreive, do some fancy stuff like this.

然后回去,做一些像这样的奇特的事情。

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