NSOperation使用
2015-08-12 20:53
281 查看
NSOperation and NSOperationQueue working thread vs main thread
多线程编程2-NSOperation
iPhone Coding – Turbo Charging Your Apps With NSOperation
如果使用如下的方式创建了NSOperationQueue,将会在后台线程中执行:
除非你在使用主线程mainQueue:
会使用如下的方式使用代码:
GCD的方式是:
可以在两个operation之间添加依赖关系。
NSInvocationOperation的使用
多线程编程2-NSOperation
iPhone Coding – Turbo Charging Your Apps With NSOperation
如果使用如下的方式创建了NSOperationQueue,将会在后台线程中执行:
NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
除非你在使用主线程mainQueue:
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
会使用如下的方式使用代码:
NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];[myQueue addOperationWithBlock:^{
// Background work
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Main thread work (UI usually)
}];
}];
GCD的方式是:
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { // Background work dispatch_async(dispatch_get_main_queue(), ^(void) { // Main thread work (UI usually) }); });
可以在两个operation之间添加依赖关系。
__block NSData *dataFromServer = nil; NSBlockOperation *downloadOperation = [[NSBlockOperation alloc] init]; __weak NSBlockOperation *weakDownloadOperation = downloadOperation; [weakDownloadOperation addExecutionBlock:^{ // Download your stuff // Finally put it on the right place: dataFromServer = .... }]; NSBlockOperation *saveToDataBaseOperation = [[NSBlockOperation alloc] init]; __weak NSBlockOperation *weakSaveToDataBaseOperation = saveToDataBaseOperation; [weakSaveToDataBaseOperation addExecutionBlock:^{ // Work with your NSData instance // Save your stuff }]; [saveToDataBaseOperation addDependency:downloadOperation]; [myQueue addOperation:saveToDataBaseOperation]; [myQueue addOperation:downloadOperation];
NSInvocationOperation的使用
- (void) loadData { /* Operation Queue init (autorelease) */ NSOperationQueue *queue = [NSOperationQueue new]; /* Create our NSInvocationOperation to call loadDataWithOperation, passing in nil */ NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadDataWithOperation) object:nil]; /* Add the operation to the queue */ [queue addOperation:operation]; [operation release]; } - (void) loadDataWithOperation { NSURL *dataURL = [NSURL URLWithString:@"http://icodeblog.com/samples/nsoperation/data.plist"]; NSArray *tmp_array = [NSArray arrayWithContentsOfURL:dataURL]; for(NSString *str in tmp_array) { [self.array addObject:str]; } [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; }
相关文章推荐
- [Ptrace]Linux内存替换(五)x86_64平台代码注入
- linux学习记录
- Linux驱动开发概述
- 基于浏览器自身的event-loop特性,初探在此基础上的promise模型
- 用proc实时管理 Linux
- HDU5371——Manacher+set维护——Hotaru's problem
- 如何编写NopCommerce插件(一)建立一个MVC的项目
- 如何限制访问一些网站
- Linux环境PHP5.5以上连接SqlServer2008【全网最经典无错版】
- Linux man命令的使用方法
- OC-@property和@synthesize理解
- Linux服务器性能评估与优化、监控利器---dstat应用
- Linux服务器性能评估与优化、监控利器---dstat应用
- [SaltStack] Salt高可用和负载均衡部署
- linux桌面之菜单与文件关联简介
- mahout的安装,配置及聚类测试
- Linux下菜单编辑配置
- 解决:LNMP架构下访问php页面出现500错误
- linux内核之系统调用
- opensips-1.8.3在RHEL5.5的搭建过程