使用NSRunloop等待异步任务完成

本文出自 清风徐来,水波不兴 的博客,转载时请注明出处及相应链接。

本文永久链接: http://www.winddisk.com/2012/05/19/%e4%bd%bf%e7%94%a8n/


一. 代码及原理

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

此段代码的含义就是,暂停当前处理流程,转而去处理其他input source,因limitDate设为了[NSDate distantFuture],则除非处理完毕一个其他input source,否则永不返回。

二.简单示例

可以使用此方法等待异步任务的完成,如下NSRunloop中代码所示,当shouldKeepRunning为YES时,程序一直循环处理其他消息,当前流程不继续处理,除非其他异步任务或者其他消息处理改变了shouldKeepRunning的值,当前流程能继续。

BOOL shouldKeepRunning = YES;        // global

NSRunLoop *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所示。

参考:
iPhone/iPad – Get User-Agent for UIWebView
iPhone/iPad – Wait for asynchronous tasks to complete
NSRunLoop Class Reference
Calling -[NSRunLoop runMode:beforeDate:] on main thread causes queued NSOperations to run on main thread
best-way-to-make-nsrunloop-wait-for-a-flag-to-be-set


转载于:https://www.cnblogs.com/zsw-1993/archive/2013/01/09/4880510.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值