如何等待调度异步完成?

时间:2014-03-25 08:54:21

标签: xcode objective-c-blocks grand-central-dispatch

我使用dispatch async从facebook获取som信息,但我不知道asyn完成时间

-(void)shareScoreViaFB:(int)score{
        __block    NSDictionary * dic;
        ACAccountStore *_accStore=[[ACAccountStore alloc]init];
        ACAccountType *_accType =[_accStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
        NSDictionary *options = @{
                                  ACFacebookAppIdKey:_mAppID,
                                  ACFacebookPermissionsKey: @[@"publish_actions"],
                                  ACFacebookAudienceKey: ACFacebookAudienceFriends
                                  };

        [_accStore requestAccessToAccountsWithType:_accType options:options completion:^(BOOL granted, NSError *e) {
            if (granted) {
                NSArray *accounts = [_accStore   accountsWithAccountType:_accType];
                _accFB = [accounts lastObject];
//    [self openShareDialog];
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
                    dic =[self getListScore];
                    dispatch_sync(dispatch_get_main_queue(), ^{
                        NSLog(@"data1: %@",dic);
                    });
                });
            };
       }];
}

获取信息的方法

-(NSDictionary *)getListScore{
    __block NSDictionary * dict=nil;

    NSString *url =[NSString stringWithFormat:@"https://graph.facebook.com/%@/scores",_mAppID];
    NSURL * strURL =[NSURL URLWithString:url];
    NSDictionary * parameters =[NSDictionary dictionaryWithObjectsAndKeys:@"score,user",@"fields", nil];
    SLRequest * request =[SLRequest requestForServiceType:SLServiceTypeFacebook
                                            requestMethod:SLRequestMethodGET URL:strURL parameters:parameters];
    request.account = _accFB;
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if (!error && responseData) {
            dict = [NSJSONSerialization JSONObjectWithData:responseData     options:0      error:nil];
            NSLog(@"%@",dict[@"data"]);
            //            return dict[@"data"];

        }

    }];
    return dict;
}

当我运行'日志显示时: - dict1:null。 - dict:好的(显示真值)。

我如何等待dispatch_asyn完成并将此值设置为主界面?

1 个答案:

答案 0 :(得分:0)

尝试按以下步骤操作:

-(void)shareScoreViaFB:(int)score{
        __block    NSDictionary * dic;
        ACAccountStore *_accStore=[[ACAccountStore alloc]init];
        ACAccountType *_accType =[_accStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
        NSDictionary *options = @{
                                  ACFacebookAppIdKey:_mAppID,
                                  ACFacebookPermissionsKey: @[@"publish_actions"],
                                  ACFacebookAudienceKey: ACFacebookAudienceFriends
                                  };
        [_accStore requestAccessToAccountsWithType:_accType options:options completion:^(BOOL granted, NSError *e) {
            if (granted) {
                NSArray *accounts = [_accStore   accountsWithAccountType:_accType];
                _accFB = [accounts lastObject];

                NSString *url =[NSString stringWithFormat:@"https://graph.facebook.com/%@/scores",_mAppID];
                NSURL * strURL =[NSURL URLWithString:url];
                NSDictionary * parameters =[NSDictionary dictionaryWithObjectsAndKeys:@"score,user",@"fields", nil];
                SLRequest * request =[SLRequest requestForServiceType:SLServiceTypeFacebook
                                            requestMethod:SLRequestMethodGET URL:strURL parameters:parameters];
                request.account = _accFB;
                [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                     if (!error && responseData) {
                          dict = [NSJSONSerialization JSONObjectWithData:responseData     options:0      error:nil];
                          NSLog(@"%@",dict[@"data"]);
                          dispatch_sync(dispatch_get_main_queue(), ^{
                               NSLog(@"data1: %@",dic);
                          });
                     }

                }];
            };
       }];
}