码迷,mamicode.com
首页 > 移动开发 > 详细

iOS中用GCD和NSOperation多个异步操作的关联

时间:2016-05-22 23:11:31      阅读:352      评论:0      收藏:0      [点我收藏+]

标签:

  在iOS实际开发中,我们可能会遇到下面的场景:有以下四个操作A,B,C,D.要求A,B,C在子线程中执行,当A,B,C执行完毕之后回到主线程执行操作D,ABC之间可能会有相互依赖的关系,我们可以通过GCD和NSOperation都可以实现这样的需求.

1.GCD   

 

// 用GCD
- (void)useGCD
{
    // 1.1可以创建一个全局并发队列,A,B,C操作会在子线程中并发执行,ABC不存在先后执行顺序
//    dispatch_queue_t quque=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 1.2也可以可以创建一个串行队列,A,B,C操作会在子线程中串行执行,A-B-C依次执行
    dispatch_queue_t quque=dispatch_queue_create("com.hzsy.www", 0);

    // 2.创建一个队列组
    dispatch_group_t group=dispatch_group_create();
    
    // 3.进入队列组
    dispatch_group_enter(group);
    
    // 4.向队列组中添加队列并异步执行
    dispatch_group_async(group, quque, ^{
        NSLog(@"A--begain-%@",[NSThread currentThread]);
        NSLog(@"A--end-%@",[NSThread currentThread]);
    });
    
    dispatch_group_async(group, quque, ^{
        NSLog(@"B--begain-%@",[NSThread currentThread]);
        NSLog(@"B--end-%@",[NSThread currentThread]);
    });
    
    dispatch_group_async(group, quque, ^{
        NSLog(@"C--begain-%@",[NSThread currentThread]);
        NSLog(@"C--end-%@",[NSThread currentThread]);
    });
    
    // 5.当队列组中的操作执行结束后回到主线程执行操作D
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"D--finished-%@",[NSThread currentThread]);
    });
    
    // 6.离开队列组
    dispatch_group_leave(group);
    
    /*
        全局并发队列打印结果:
     2016-05-22 21:36:58.518 GCD和NSOpration[797:144336] A--begain-<NSThread: 0x15c51f170>{number = 2, name = (null)}
     2016-05-22 21:36:58.525 GCD和NSOpration[797:144335] B--begain-<NSThread: 0x15c556a90>{number = 3, name = (null)}
     2016-05-22 21:36:58.525 GCD和NSOpration[797:144338] C--begain-<NSThread: 0x15c54ac90>{number = 4, name = (null)}
     2016-05-22 21:36:58.525 GCD和NSOpration[797:144336] A--end-<NSThread: 0x15c51f170>{number = 2, name = (null)}
     2016-05-22 21:36:58.525 GCD和NSOpration[797:144335] B--end-<NSThread: 0x15c556a90>{number = 3, name = (null)}
     2016-05-22 21:36:58.525 GCD和NSOpration[797:144338] C--end-<NSThread: 0x15c54ac90>{number = 4, name = (null)}
     2016-05-22 21:36:58.526 GCD和NSOpration[797:144001] D--finished-<NSThread: 0x15c50c380>{number = 1, name = main}
     */
    
    /*
        串行队列打印结果:
     2016-05-22 21:39:20.660 GCD和NSOpration[805:145035] A--begain-<NSThread: 0x15c599cd0>{number = 2, name = (null)}
     2016-05-22 21:39:20.670 GCD和NSOpration[805:145035] A--end-<NSThread: 0x15c599cd0>{number = 2, name = (null)}
     2016-05-22 21:39:20.670 GCD和NSOpration[805:145035] B--begain-<NSThread: 0x15c599cd0>{number = 2, name = (null)}
     2016-05-22 21:39:20.670 GCD和NSOpration[805:145035] B--end-<NSThread: 0x15c599cd0>{number = 2, name = (null)}
     2016-05-22 21:39:20.671 GCD和NSOpration[805:145035] C--begain-<NSThread: 0x15c599cd0>{number = 2, name = (null)}
     2016-05-22 21:39:20.671 GCD和NSOpration[805:145035] C--end-<NSThread: 0x15c599cd0>{number = 2, name = (null)}
     2016-05-22 21:39:20.671 GCD和NSOpration[805:145012] D--finished-<NSThread: 0x15c604ba0>{number = 1, name = main}
     */
}

 

2.NSOperationQueue  

 1 @interface ViewController ()
 2 @property (nonatomic, strong) NSOperationQueue *queue;
 3 @end
 4 
 5 @implementation ViewController
 6 
 7 - (void)viewDidLoad {
 8     [super viewDidLoad];
 9     NSOperationQueue *queue= [[NSOperationQueue alloc]init];
10     _queue=queue;
11 }
12 @end

 

// 用NSOperationQueue
- (void)useOperationQueue
{
    // 1.创建ABCD执行block
    void (^blockA)(void)=^(void){
        NSLog(@"A--begain-%@",[NSThread currentThread]);
        NSLog(@"A--end-%@",[NSThread currentThread]);
    };
    
    void (^blockB)(void)=^(void){
        NSLog(@"B--begain-%@",[NSThread currentThread]);
        NSLog(@"B--end-%@",[NSThread currentThread]);
    };
    
    void (^blockC)(void)=^(void){
        NSLog(@"C--begain-%@",[NSThread currentThread]);
        NSLog(@"C--end-%@",[NSThread currentThread]);
    };
    
    void (^blockD)(void)=^(void){
        NSLog(@"D--finished-%@",[NSThread currentThread]);
    };
    
    // 2.创建ABCD的NSBlockOperation操作block
    NSBlockOperation *opA=[NSBlockOperation blockOperationWithBlock:blockA];
    NSBlockOperation *opB=[NSBlockOperation blockOperationWithBlock:blockB];
    NSBlockOperation *opC=[NSBlockOperation blockOperationWithBlock:blockC];
    NSBlockOperation *opD=[NSBlockOperation blockOperationWithBlock:blockD];
    
    // 3.1设置D操作的依赖
    [opD addDependency:opA];
    [opD addDependency:opB];
    [opD addDependency:opC];
    
    // 3.2设置ABC之间的依赖,如果不设置的话ABC将并发执行
    [opB addDependency:opA];
    [opC addDependency:opB];
    
    // 4.添加ABC操作到队列
    [_queue addOperation:opA];
    [_queue addOperation:opB];
    [_queue addOperation:opC];
    
    //5.添加D到主队列
    [[NSOperationQueue mainQueue] addOperation:opD];
    /*
        设置ABC之间的依赖执行结果执行结果:
     2016-05-22 21:55:29.114 GCD和NSOpration[828:148402] A--begain-<NSThread: 0x147556cc0>{number = 2, name = (null)}
     2016-05-22 21:55:29.127 GCD和NSOpration[828:148402] A--end-<NSThread: 0x147556cc0>{number = 2, name = (null)}
     2016-05-22 21:55:29.128 GCD和NSOpration[828:148402] B--begain-<NSThread: 0x147556cc0>{number = 2, name = (null)}
     2016-05-22 21:55:29.128 GCD和NSOpration[828:148402] B--end-<NSThread: 0x147556cc0>{number = 2, name = (null)}
     2016-05-22 21:55:29.130 GCD和NSOpration[828:148402] C--begain-<NSThread: 0x147556cc0>{number = 2, name = (null)}
     2016-05-22 21:55:29.130 GCD和NSOpration[828:148402] C--end-<NSThread: 0x147556cc0>{number = 2, name = (null)}
     2016-05-22 21:55:29.130 GCD和NSOpration[828:148377] D--finished-<NSThread: 0x14750c3e0>{number = 1, name = main}
     
     */
    /*
        不设置ABC之间的依赖执行结果:
     2016-05-22 21:56:44.480 GCD和NSOpration[833:148926] A--begain-<NSThread: 0x1265a3730>{number = 2, name = (null)}
     2016-05-22 21:56:44.480 GCD和NSOpration[833:148929] B--begain-<NSThread: 0x12658f4a0>{number = 3, name = (null)}
     2016-05-22 21:56:44.480 GCD和NSOpration[833:148927] C--begain-<NSThread: 0x1265a2ea0>{number = 4, name = (null)}
     2016-05-22 21:56:44.484 GCD和NSOpration[833:148926] A--end-<NSThread: 0x1265a3730>{number = 2, name = (null)}
     2016-05-22 21:56:44.485 GCD和NSOpration[833:148929] B--end-<NSThread: 0x12658f4a0>{number = 3, name = (null)}
     2016-05-22 21:56:44.485 GCD和NSOpration[833:148927] C--end-<NSThread: 0x1265a2ea0>{number = 4, name = (null)}
     2016-05-22 21:56:44.485 GCD和NSOpration[833:148902] D--finished-<NSThread: 0x12650c3c0>{number = 1, name = main}
     */
    
}

从上面的打印结果可以看到ABC之间如果设置了依赖关系,只会创建一条子线程.如果不设置会创建多条子线程.

 

 

 

 

 

---恢复内容结束---

iOS中用GCD和NSOperation多个异步操作的关联

标签:

原文地址:http://www.cnblogs.com/zhahao/p/5517986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!