使用NSRunloop等待异步任务完成
来源:互联网 发布:oracle数据库认证考试 编辑:程序博客网 时间:2024/06/05 04:31
一. 代码及原理
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
- [NSDate distantFuture] 表示遥远的将来,基本不会到达。
- + (NSRunLoop *)currentRunLoop获取当前线程的NSRunLoop对象。
- - (BOOL)runMode:(NSString *)mode beforeDate:(NSDate *)limitDate 在指定模式下处理输入源(input source),在首个输入源(input source)处理完毕或者等待时间limitDate到达时退出。
此段代码的含义就是,暂停当前处理流程,转而去处理其他input source,因limitDate设为了[NSDate distantFuture],则除非处理完毕一个其他input source,否则永不返回。
二.简单示例
可以使用此方法等待异步任务的完成,如下NSRunloop中代码所示,当shouldKeepRunning为YES时,程序一直循环处理其他消息,当前流程不继续处理,除非其他异步任务或者其他消息处理改变了shouldKeepRunning的值,当前流程能继续。
BOOL shouldKeepRunning = YES; // globalNSRunLoop *theRL = [NSRunLoop currentRunLoop];while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
三.实际使用
一个更实际一点的例子,Björn Sållarp的获取UIWebView的user-agent字符串的代码。
BSWebViewUserAgent.h
#import @interface BSWebViewUserAgent : NSObject {NSString *userAgent;UIWebView *webView;}@property (nonatomic, retain) NSString *userAgent;-(NSString*)userAgentString;@end
BSWebViewUserAgent.m
#import "BSWebViewUserAgent.h"@implementation BSWebViewUserAgent@synthesize userAgent;-(NSString*)userAgentString{webView = [[UIWebView alloc] init];webView.delegate = self;[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"www.google.com"]]];// Wait for the web view to load our bogus request and give us the secret user agent.while (self.userAgent == nil){// This executes another run loop.[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];}return self.userAgent;}-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{self.userAgent = [request valueForHTTPHeaderField:@"User-Agent"];// Return no, we don't care about executing an actual request.return NO;}- (void)dealloc{[webView release];[userAgent release];[super dealloc];}@end
在UIWebView中load一个URL,然后使用NSRunloop在等待,在UIWebView的shouldStartLoadWithRequest回调函数中获取到了user Agent字符串,使得主流程可以继续。而shouldStartLoadWithRequest返回的是NO,所以网页实际上未加载。
此代码的使用方式如下,看起来时同步处理,其中隐含着异步请求以及循环等待。
BSWebViewUserAgent *agent = [[BSWebViewUserAgent alloc] init];NSLog(@"User-agent: %@", [agent userAgentString]);[agent release];
四.谨慎使用
除非你确切知道你在做什么,否则别这么用。
手工操作NSRunloop可能会造成意想不到的结果,如Calling -[NSRunLoop runMode:beforeDate:] on main thread causes queued NSOperations to run on main thread所示。
0 0
- 使用NSRunloop等待异步任务完成
- 使用NSRunloop等待异步任务完成
- 使用NSRunloop等待异步任务完成
- 使用NSRunloop等待异步任务完成
- 使用异步任务完成应用更新
- Django中如何使用django-celery完成异步任务
- Django中如何使用django-celery完成异步任务 (1)
- Django中如何使用django-celery完成异步任务 (2)
- jango中如何使用django-celery完成异步任务 (1)
- Django中如何使用django-celery完成异步任务 (2)
- PHP使用gearman扩展完成异步任务总结
- Django中使用django-celery完成异步任务(1)
- 限时等待某一任务完成示例
- VB.NET实现任务异步等待功能
- objective-c 等待多个异步函数完成后,再执行某段代码--GCD Group 的使用
- 异步任务AsyncTask使用
- 异步任务AsyncTask使用
- 等待JQuery异步全部完成执行语句(函数)
- FMS应用实例 - 从FMS服务器读取文件(图片/SWF/文本)到客户端
- 文件系统Minifilter驱动(五)
- 使用命令行的方式操作Xcode
- MongoDB 数据库连接字符串
- 文件系统Minifilter驱动(六)
- 使用NSRunloop等待异步任务完成
- Struts2的Action配置通配符
- Kafka简要图解
- 内部排序 有空看看
- 四种加密算法之AES的源码-C++
- Call to undefined function curl_init()
- 文件系统Minifilter驱动(七)
- 堆和栈的区别
- OSGI