客观C / Parse查询中的块

时间:2015-02-08 09:48:27

标签: ios objective-c parse-platform objective-c-blocks

我有一个使用解析查询获取成员列表的函数。我有一个NSMutableArray类型的属性,它应该存储名为 subscriber 的列表结果。我在代码中有两个块。一个块获取日期,一旦我有日期,我调用另一个函数,进一步调用解析云方法。这是代码

-(void) fetchMemberList{

PFQuery *query=[PFQuery queryWithClassName:@"GroupMembers"];
[query fromLocalDatastore];
[query orderByAscending:@"updatedAt"];
[query whereKey:@"iosUserID" equalTo:[PFUser currentUser].objectId];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if(error)
{

}
else{

    if(objects.count==0) {
        _latestTime=[PFUser currentUser].createdAt;
    }

    else {
        PFObject *member=[objects objectAtIndex:0];
        _latestTime =member.createdAt;
    }

    [Data getMemberList:_latestTime successBlock:^(id object) {

        NSMutableDictionary *members = (NSMutableDictionary *) object;

        NSArray *appUser= [members objectForKey:@"app"];
        NSArray *phoneUser=[members objectForKey:@"sms"];
        if(appUser.count>0){
        for(PFObject * appUs in appUser)
        {
            appUs[@"iosUserID"]=[PFUser currentUser].objectId;
            NSString *obj= [appUs objectForKey:@"name"];
            NSString *child= [appUs objectForKey:@"childern_names"];
            if(child.length>0)
            {
                [_subscriber addObject:child];
            }
            else if(obj.length>0)
            {
                [_subscriber addObject:obj];

            }


            }
            [PFObject pinAllInBackground:appUser];

        }

        if(phoneUser.count>0){
        for(PFObject * phoneUs in phoneUser)
        {
            phoneUs[@"iosUserID"]=[PFUser currentUser].objectId;
            NSString *obj= [phoneUs objectForKey:@"subscriber"];
            NSString *child= [phoneUs objectForKey:@"number"];
            if(child.length>0)
            {
                [_subscriber addObject:child];
            }
            else if(obj.length>0)
            {
                [_subscriber addObject:obj];

            }

        }
            [PFObject pinAllInBackground:phoneUser];

        }


       NSLog(@"%@ subscriber in fetch method",_subscriber);
    } errorBlock:^(NSError *error) {

    }];


}


}];


}

在我的 viewdidload 功能中: -

- (void)viewDidLoad {
[super viewDidLoad];
_subscriber=[[NSMutableArray alloc]init];


[self fetchMemberList];
NSLog(@"%@ in viewdidLoad",_subscriber);

}

因此,viewdidLoad中的日志不会打印任何内容,但方法中的日志会显示名称列表。我想将列表存储在数组中并将其放入tableview。我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

findObjectsInBackgroundWithBlock:块是异步的,因此_subscriber isn't immediately available to print after you call your fetchMemberList`方法。这就是为什么块内的那个实际上为你提供了名单 - 因为到那时回调已经完成了。