将对象添加到PFRelation时出错,这是怎么做到的?

时间:2014-12-19 10:39:55

标签: ios xcode parse-platform pfrelation

我试图解析PFRelation中的问题。我有一个名为“girlBio”的课程,它存储有关女孩的信息,还有一个名为“stuff”的课程,用于存储有关项目的信息。代码如下:

PFObject *item = [PFObject objectWithClassName:@"stuff"];
item[@"Name"] = @"PS3";
PFObject *girl = [PFObject objectWithClassName:@"girlBio"];
girl[@"Name"] = @"Jessica";
PFObject *girl2 = [PFObject objectWithClassName:@"girlBio"];
girl2[@"Name"] = @"Cindy";

PFRelation *relation = [item relationForKey:@"owners"];
[relation addObject:girl];
[relation addObject:girl2];
[item saveInBackground];

---------------------------------更新也尝试了这个---------- ---------------

PFObject *item = [PFObject objectWithClassName:@"stuff"];
item[@"Name"] = @"PS3";
PFObject *girl = [PFObject objectWithClassName:@"girlBio"];
girl[@"Name"] = @"Jessica";
[item saveInBackground];
[girl saveInBackground];
PFRelation *relation = [item relationForKey:@"owners"];
[relation addObject:girl];
[item saveInBackground];

所以我希望这个项目由几个女孩拥有,但是当我运行程序时,我收到了这个错误:

错误:无法向关系添加非指针(代码:111,版本:1.6.0)

有人可以帮忙吗?

谢谢

1 个答案:

答案 0 :(得分:8)

您需要在保存关系之前保存对象girl1girl2。否则,即使您认为本地副本知道它们,服务器也不会。

更新

在保存关系之前,您还需要确保完成girl1girl2以及item的保存。但是,您可能不希望在主线程上运行这些保存,所以我建议这样的(我刚刚运行时没有问题):

dispatch_async(dispatch_get_main_queue(), ^{
    PFObject *item = [PFObject objectWithClassName:@"stuff"];
    item[@"Name"] = @"PS3";
    PFObject *girl = [PFObject objectWithClassName:@"girlBio"];
    girl[@"Name"] = @"Jessica";
    [item save];
    [girl save];
    PFRelation *relation = [item relationForKey:@"owners"];
    [relation addObject:girl];
    [item saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        //Do something after the last save...
    }];
});