Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios pf关系赢得';不要在Parse.com上保存_Ios_Objective C_Xcode_Parse Platform - Fatal编程技术网

Ios pf关系赢得';不要在Parse.com上保存

Ios pf关系赢得';不要在Parse.com上保存,ios,objective-c,xcode,parse-platform,Ios,Objective C,Xcode,Parse Platform,我在保存PFRelation时遇到问题我有以下代码: //set up the query PFQuery *query = [PFQuery queryWithClassName:@"messageBank"]; [query whereKey:@"username" equalTo:name]; __weak User *weakSelf = self; [query getFirstObjectInBackgroundWithBlock:^(PFObje

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

//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 friends。它甚至不尝试调用
[weakSelf.messageBank saveinbackgroundithblock…
.weakSelf.messageBank是本地PFObject,weakSelf.friends是PFRelation。有人知道这里会出什么问题吗?如果我在类a中有一个PFRelation,在该关系中有指向类a中其他对象的指针可以吗?它需要在不同的类中吗?有吗非常感谢您的帮助!!!

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

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指针的弱副本(尽管这样做没有坏处)。该习惯用法用于避免self和块的所有者之间的retain循环。仅当self是块的所有者(直接或间接)时才需要它。parse的完成块不是这种情况。

这段代码在一个名为User的类中?这是一个PFUser子类吗?这个类有一个名为friendsRelation的关系和一个名为friends的关系,您希望将找到的messageBank添加到这两个类中?是什么让您确信weakSelf.messageBank有任何价值?(nil可以解释save的非调用)。既然代码更改的对象是weakSelf,为什么不保存weakSelf呢?(另外,这里不需要weakSelf复制习惯用法。self会做得很好,不会泄漏)。也许可以描述数据模型和目标,应该很容易实现。