2013-08-28 59 views
1

我試圖理解Objective-C中的塊。舉例來說,登錄到Twitter。我希望主線程等待塊完成。瞭解塊(使用twitter登錄)

如何在下面的代碼中實現此目的。 showMain方法正在轉換到下一個視圖。數組TweetArray爲空,因爲異步塊沒有完成。現在,我使用方法performSelectorOnMainThread:withObject:waitUntilDone :.請詳細解釋塊的概念。

[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) 
    { 
     [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
      if (granted) 
      { 
       NSArray *accounts = [account accountsWithAccountType:accountType]; 
       // Check if the users has setup at least one Twitter account 
       if (accounts.count > 0) 
       { 
        ACAccount *twitterAccount = [accounts objectAtIndex:0]; 

        SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"] parameters:[NSDictionary dictionaryWithObject:twitterAccount.username forKey:@"screen_name"]]; 
        [twitterInfoRequest setAccount:twitterAccount]; 

        // Making the request 
        [twitterInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
         dispatch_async(dispatch_get_main_queue(), ^{ 
          if ([urlResponse statusCode] == 429) { 
           return; 
          } 
          if (error) { 
           return; 
          } 
          // Check if there is some response data 
          if (responseData) { 
           NSError *error = nil; 
           NSArray *TweetArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error]; 

           NSLog(@"%@", TweetArray); 
          } 
         }); 
        }]; 
       } 
      } else { 
       NSLog(@"Access denied"); 
      } 
     }); 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [MBProgressHUD hideHUDForView:self.view animated:YES]; 

      [self performSelectorOnMainThread:@selector(showMain) withObject:nil waitUntilDone:YES]; 
     }); 
    }]; 

謝謝, 邁克

+1

*請詳細解釋塊的概念* ***略微***太寬,無法正確回答。抓住一本Objective-C書,閱讀塊章節,回來一個更狹窄的問題。 –

回答

1

解釋塊的整體概念,在短短的答案被問多了,你應該閱讀文檔和一些書籍搜索。

而是直接找你的問題,我會盡量告訴這是怎麼回事:

這從twitterInfoRequest對象方法performRequestWithHandler:,好像是這個名字已經說,執行徵用到Twitter的服務器,返回responseData。該方法接收一個塊作爲參數,該塊將在請求到達時正確調用。所以,這個塊只不過是一段代碼,當你收到它時,它將被執行到handleresponseData

所以,如果你想在之後做點什麼,那麼你必須把它放在這個塊內。對你來說,這將是調用showMain方法:

(...) 
[twitterInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
        dispatch_async(dispatch_get_main_queue(), ^{ 
         if ([urlResponse statusCode] == 429) { 
          return; 
         } 
         if (error) { 
          return; 
         } 
         // Check if there is some response data 
         if (responseData) { 
          NSError *error = nil; 
          NSArray *TweetArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error]; 

          NSLog(@"%@", TweetArray); 

          [self showMain]; 
         } 
        }); 
       }]; 

PS:因爲你已經在主線程內執行該手柄塊(與dispatch_async(dispatch_get_main_queue()...)你不必通過performSelectorOnMainThread調用該方法。