【发布时间】: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