2014-03-25 27 views
0

我曾經派遣異步獲取來自Facebook索姆信息,但我不知道什麼時候ASYN完成如何等待派遣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。 - 字典:好的(這顯示真正的價值)。

我如何等待dispatch_asyn完成並將此值設置爲主界面?

回答

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

       }]; 
      }; 
     }]; 
} 
+0

謝謝你,我想回[DIC]數據和另一種觀點認爲,用它來顯示信息,我該怎麼辦呢? – user3200622

+0

爲你的其他視圖添加一個方法' - (void)updateWithDict:(NSDictionary *)dict;'並且調用它而不是'NSLog(@「data1:%@」,dic);'''myview updateWithDict:dic] ;' – MuhammadBassio