首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PFRelation不会在Parse.com上保存

PFRelation不会在Parse.com上保存
EN

Stack Overflow用户
提问于 2015-03-07 00:47:31
回答 1查看 254关注 0票数 0

我在保存PFRelation时遇到了问题,我有以下代码:

代码语言:javascript
复制
//set up the query
    PFQuery *query = [PFQuery queryWithClassName:@"messageBank"];
    [query whereKey:@"username" equalTo:name];
    __weak User *weakSelf = self;


    [query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {

        if(error) {

            NSLog(@"No such user");
            handler(NO, error,NO,NO);
        }

        else{

            [weakSelf.friendsRelation addObject:object];
            [weakSelf.friends addObject:object];
            //save in the background
            [weakSelf.messageBank saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if(error) {

                    NSLog(@"Save error");
                }

                else {

                    NSLog(@"no error");
                }
            }];


            handler(YES,nil,NO,NO); //no errors
            //so the friend is added to the friends array, all we need to do is reload the table data don't need to init the array again, the relation is also added to the relation item so don't need to init that again
        }


    }];//end block

我的代码发现messageBank对象很好,但是它不会将它保存到PFRelation好友。它甚至不会尝试调用[weakSelf.messageBank saveInBackgroundWithBlock...。weakSelf.messageBank是本地的PFObject,weakSelf.friends是它的PFRelation。有人知道这里会出什么问题吗?如果我在类A中有一个PFRelation,是否可以在这个关系中包含指向类A中其他对象的指针?它是否需要在不同的类中?任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2015-03-07 01:46:09

这是一个经过清理的代码版本,它获取一个对象并添加到它的关系中,然后保存它……

代码语言:javascript
复制
PFQuery *query = [PFQuery queryWithClassName:@"messageBank"];  // by convention class names should be capital MessageBank, but using yours
[query whereKey:@"username" equalTo:name];  // better form is self.name assuming it is an attribute of self

[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (!error) {
        // see note below about weakSelf
        // assume self is a PFObject subclass with two relations
        // (and generated setters) called friendsRelation and friends
        [self.friendsRelation addObject:object];
        [self.friends addObject:object];

        // notice we save self here.  that's who changed in the two preceding lines
        [self saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if(!error) {
                // success
            } else {
                // handle error
            }
        }];
    } else {
        // handle error
    }
}];

请注意,不需要声明self指针的__weak副本(不过,它没有什么坏处)。这个习惯用法是用来避免在self和块的所有者之间的保留循环。只有当self是块所有者时才需要它(直接或间接)。这不是parse的完成块的情况。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28903550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档