用ASIHttpRequest进行异步加载数据时,加载成功后会调用代理函数
|
1
|
- (void)requestFinished:(ASIHTTPRequest *)request |
开始我以为会在当前线程中执行,后来发现有时是,有时会跑到主线程中执行,这问题一直纠结至今。
今天使劲google一番,终于得到结果,在ASIHTTPRequest.m文件中有这么一段
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
- (void)requestFinished{#if DEBUG_REQUEST_STATUS || DEBUG_THROTTLING NSLog(@"[STATUS] Request finished: %@",self);#endif if
([self
error] || [self
mainRequest]) { return; } if
([self
isPACFileRequest]) { [self
reportFinished]; }
else { [self
performSelectorOnMainThread:@selector(reportFinished)
withObject:nil
waitUntilDone:[NSThread
isMainThread]]; }} |
不使用PACFileRequest即自动化脚本(PAC是个啥东西?自己google去吧 – -)时,会到主线程中去执行函数reportFinished,这个函数会进而调用代理函数:
|
1
|
- (void)requestFinished:(ASIHTTPRequest *)request |
还是不要刻意到主线程中去调用的好,所以直接把自动脚本判断语句删掉,只需要[self reportFinished]就行了。
本文深入解析ASIHttpRequest如何实现异步加载数据,并详细解释了加载成功后的回调函数执行机制,包括在当前线程或主线程中执行的情况,以及如何优化回调函数的执行环境。
1635

被折叠的 条评论
为什么被折叠?



