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

你可能感兴趣的:(IOS)