如何在不创建全新PFObject的情况下向PFObject添加信息?

时间:2013-08-27 09:25:37

标签: iphone ios objective-c xcode cocoa

我现在明白了,感谢@dbarthelemy。但是这里现在是我的代码,我收到错误/终止no"结果与查询匹配":

- (IBAction)createGroup:(id)sender {
    PFObject *message = [PFObject objectWithClassName:@"Messages"];
    [message setObject:self.recipients forKey:@"recipientIds"];
    [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
        else [self performSegueWithIdentifier:@"showDate" sender:self];
    }];
}

然后在第二个控制器上:

PFQuery *message = [PFQuery queryWithClassName:@"Message"];
            [message getObjectInBackgroundWithId:@"recipientIds" block:^(PFObject     *message, NSError *error) {
                [message setObject:file forKey:@"file"];
                [message setObject:fileType forKey:@"fileType"];
                [message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
                [message setObject:[[PFUser currentUser] username] forKey:@"senderName"];
                [message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

1 个答案:

答案 0 :(得分:0)

在createGroup:方法中,请确保使用其中一种保存方法将PFObject保存到Parse(例如saveInBackgroundWithBlock :)。

- (IBAction)createGroup:(id)sender {
PFObject *message = [PFObject objectWithClassName:@"Message"];
[message setObject:self.recipients forKey:@"recipientIds"];
[message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

然后使用单独的viewController类(使用属性)共享PFObject,然后使用此指针设置其他PFObject属性(而不是使用objectWithClassName创建新的PFObject :)。 设置了其他属性后,不要忘记保存更新的PFObject。

[self.message setObject:file forKey:@"file"];
[self.message setObject:fileType forKey:@"fileType"];
[self.message setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
[self.message setObject:[[PFUser currentUser] username] forKey:@"senderName"];
[self.message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

注意:如果您无法在各种视图控制器之间可靠地共享PFObject指针,则可以在Parse服务器上执行PFQuery以检索您的对象(假设您可以构建正确的查询)。