博客> GCD的一些操作和NSOperation
GCD的一些操作和NSOperation
2018-10-17 13:59 评论:10 阅读:1097 jijing2013
ios GCD&NSOperation

GCD的其他操作 1、//GCD应用,单例模式

  • (void)gcdTest1{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSLog(@"执行一次%@",[NSThread currentThread]); }); } 2、 //延迟操作
  • (void)gcdTest2{ //第一种 [self performSelector:@selector(handleAction) withObject:nil afterDelay:2]; //第二种 dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));

    dispatch_after(time, dispatch_get_main_queue(), ^{ NSLog(@"%@",[NSThread currentThread]); }); } 3、 //队列调度1

  • (void)gcdTest3{ //创建调度组 dispatch_group_t group = dispatch_group_create(); //获取全局队列 dispatch_queue_t queue = dispatch_get_global_queue(0, 0);

    //调度组的异步请求 dispatch_group_async(group, queue, ^{ NSLog(@"下载第一张图"); }); dispatch_group_async(group, queue, ^{ NSLog(@"下载第2张图"); }); dispatch_group_async(group, queue, ^{ NSLog(@"下载第3张图"); }); dispatch_group_async(group, queue, ^{ NSLog(@"下载第4张图"); }); //notify通知。当所有异步请求完成时调用dispatch_group_notify dispatch_group_notify(group, queue, ^{ NSLog(@"更新UI"); }); } 4、 //队列调度2

  • (void)gcdTest4{ //创建调度组 dispatch_group_t group = dispatch_group_create(); //获取全局队列 dispatch_queue_t queue = dispatch_get_global_queue(0, 0); dispatch_group_enter(group); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; NSLog(@"下载第一张图"); dispatch_group_leave(group); }); dispatch_group_enter(group); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:3]; NSLog(@"下载第2张图"); dispatch_group_leave(group); }); dispatch_group_enter(group); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"下载第3张图"); dispatch_group_leave(group); }); //等待调度队列wait相当于一个阻塞状态 dispatch_group_wait(group, DISPATCH_TIME_FOREVER); NSLog(@"更新UI"); } 5、//中断操作
  • (void)gcdTest5{ //获取全局队列 // dispatch_queue_t queue = dispatch_get_global_queue(0, 0); //中断操作必须使用自己创建的队列 dispatch_queue_t queue = dispatch_queue_create("com", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; NSLog(@"1"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"2"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:3]; NSLog(@"3"); }); //中断操作dispatch_barrier_async dispatch_barrier_async(queue, ^{ NSLog(@"------"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"4"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:4]; NSLog(@"5"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; NSLog(@"6"); }); } 6、//遍历次数
  • (void)gcdTest6{ dispatch_queue_t queue = dispatch_get_global_queue(0, 0); //iterations 遍历次数 dispatch_apply(5, queue, ^(size_t i) { NSLog(@"%@",@(i)); }); }

(二)NSOperation方法 1、 NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downImage) object:nil]; // 如果我们想在一个NSOperation执行完毕后做一些事情,就调用NSOperation的setCompletionBlock方法来设置想做的事情 [op setCompletionBlock:^{ NSLog(@"%@",[NSThread currentThread]); }]; // 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的。 [op start];

2、//NSBlockOperation //创建线程

  • (void)opTest2{ // 能够并发地执行一个或多个block对象,所有相关的block都执行完之后,操作才算完成 NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"%@",[NSThread currentThread]); }]; // 通过addExecutionBlock方法添加block操作,开启多个线程 [op addExecutionBlock:^{ NSLog(@"%@",[NSThread currentThread]); NSLog(@"第一个操作"); }]; [op addExecutionBlock:^{ NSLog(@"%@",[NSThread currentThread]); NSLog(@"第二个操作"); }]; [op start]; }

3、//NSOperationQueue队列创建线程

  • (void)opTest3{ //NSOperation是基于GCD的,把GCD的block封装成opertion,NSOperationQueue是全局队列封装 //将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步执行的。 NSOperationQueue queue = [[NSOperationQueue alloc] init]; NSInvocationOperation op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downImage) object:nil]; NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"op2-%@",[NSThread currentThread]); }]; //添加NSOperation到NSOperationQueue中 //NSOperation添加到queue之后,通常短时间内就会得到运行。 // [queue addOperation:op]; // [queue addOperation:op2];

    // waitUntilFinished yes 操作完成后执行下面的代码 no 先执行下面的代码

    //添加一个block形式的operation [queue addOperationWithBlock:^{ NSLog(@"op3-%@",[NSThread currentThread]); }]; [queue addOperations:@[op1, op2] waitUntilFinished:NO]; } 4、设置属性 //最大并发数,这个功能NSOperation比较常用 self.queue.maxConcurrentOperationCount = 2; //挂起状态 self.queue.suspended = !self.queue.suspended; // 取消queue中所有的操作 [self.queue cancelAllOperations];

5、//设置依赖关系

  • (void)opTest4{ NSInvocationOperation op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downImage) object:nil]; //数据量少看不出 op1.queuePriority = -3; NSBlockOperation op2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"解压图片包"); }]; op2.queuePriority = 0; NSBlockOperation * op3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"观看漫画"); }]; op3.queuePriority = 4; //添加依赖关系 [op2 addDependency:op1]; [op3 addDependency:op2];

    [self.queue addOperations:@[op1, op2, op3] waitUntilFinished:YES]; } 6、GCD和NSOperation各自特点 /* CGD - (iOS4.0)多线程解决方案 将任务(block)添加到队列(串行,并行(全局队列)),指定执行任务的方法。(同步、异步)拿到dispatch_get_main_queue,线程之间的通信

NSOperation--(iOS2.0) 将操作添加到队列,[NSOperationQueue mainQueue] 获取主队列,将任务添加到主队列,就会在线程中执行。NSOperation可以设置最大并发数,可以暂停、继续 也就是挂起操作,取消所有任务,设置依赖关系

*/- 1. list text here

收藏
0
sina weixin mail 回到顶部