sendAsynchronousRequest

相信不少人都在NSURLConnection中看過這個http請求方式

+ (void)sendAsynchronousRequest:(NSURLRequest *)request
                          queue:(NSOperationQueue*) queue
              completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*)) handler NS_AVAILABLE(10_7, 5_0);

這個方法是個多線程的異步請求方法,但是會讓界面block住, 處理它浪費了我幾個小時才處理好它。

解決這個問題的方式有兩種:

1. 如果允許的情況下采用同步請求,

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;

2. All UI operations must be done on the main thread. Multiple ways to do this

(所有的block操作放到主線程, 大多數的做法如下)

在調整界面的代碼部分,按如下方式

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
    activityIndicator.hidden = TRUE;
});


請爲這個方法發愁的朋友別擔心,我已經嘗試了,而且有效。


更多請參考:

http://stackoverflow.com/questions/8169774/sendasynchronousrequest-with-tableview-reloaddata-delay-on-draw

http://stackoverflow.com/questions/9409211/nsurlconnection-sendasynchronousrequestqueuecompletionhandler-making-multiple

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章