【问题标题】:How to wait dispatch asyn finish?如何等待调度异步完成?
【发布时间】:2014-03-25 08:54:20
【问题描述】:

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

-(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:空。 - dict: OK(这显示真实值)。

我如何等待 dispatch_asyn 完成并将这个值设置到主界面?

【问题讨论】:

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


    【解决方案1】:

    尝试如下操作:

    -(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);
                              });
                         }
    
                    }];
                };
           }];
    }
    

    【讨论】:

    • 谢谢,我要返回 [dic] 数据并在另一个视图中,用它来显示信息,我该怎么做?
    • 将方法添加到您的其他视图-(void)updateWithDict:(NSDictionary *)dict; 并调用它而不是NSLog(@"data1: %@",dic); 作为[myview updateWithDict:dic];
    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 2021-11-13
    • 2014-04-03
    • 2011-10-04
    相关资源
    最近更新 更多