NSOperation使用
发布日期:2022-03-18 08:27:34
浏览次数:26
分类:技术文章
本文共 2352 字,大约阅读时间需要 7 分钟。
如果使用如下的方式创建了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];}
转载地址:https://windzen.blog.csdn.net/article/details/47451005 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月19日 05时27分57秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Polar bear and air-conditioner
2019-04-26
不加注解引起的500错误
2019-04-26
PB使用专用接口连接SQL Server
2019-04-26
PB中创建需要带参数查询的DataWindow
2019-04-26
idea如何设置自动换行
2019-04-26
Java—JDBC入门
2019-04-26
java各工具类Utils
2019-04-26
java8 集合分组(根据集合中对象中时间字段的月份分组)
2019-04-26
Linux常用快捷键和基本命令
2019-04-26
Linux上安装jdk1.8和配置环境变量
2019-04-26
A表中存有B表的多个主键,逗号隔开,B表进行删除时,要判断B表是否绑定A,怎么实现?
2019-04-26
centos环境下docker安装redis并挂载外部配置和数据
2019-04-26
maven中的setting.xml 配置文件
2019-04-26
MySQL的Limit详解
2019-04-26
java \t,\n,\r,\b,\f 的作用
2019-04-26
java8 LocalDate 根据时间获取星期几
2019-04-26
Base64 加密解密
2019-04-26
Excel表格身份证号显示不完整问题
2019-04-26