序列化异步方法 [英] Serializing asynchronous methods

查看:110
本文介绍了序列化异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多任务需要串行执行,但是该任务包括完成块中的下一个块.

I have a number of tasks I need to execute serially but the task includes next block in a completion block.

什么是一次完成一次任务,在当前任务完成其完成任务之后又开始下一个任务的好的技术?

What is a good technique for doing these tasks one at a time, starting the next task after the current one completes its completion block?

除了具有串行NSOperationQueue的NSOperation子类之外,是否还有其他技术?

Is there a technique other than a NSOperation subclass with a serial NSOperationQueue?

推荐答案

标准解决方案:

  1. NSOperationQueue,其中maxConcurrentOperationCount1.您说您不想这样做,但您不说原因.串行队列是最合乎逻辑的解决方案.

  1. NSOperationQueue with maxConcurrentOperationCount of 1. You say you don't want to do that, but you don't say why. Serial queues are the most logical solution.

例如:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;

[queue addOperationWithBlock:^{
    NSLog(@"Starting #1");
    sleep(3);
    NSLog(@"Finishing #1");
}];

[queue addOperationWithBlock:^{
    NSLog(@"Starting #2");
    sleep(3);
    NSLog(@"Finishing #2");
}];

[queue addOperationWithBlock:^{
    NSLog(@"Starting #3");
    sleep(3);
    NSLog(@"Finishing #3");
}];

  • 如果您不想使用串行NSOperationQueue,则可以使用标准并发队列,而只需使每个操作都依赖于前一个即可.您无需使用串行队列即可实现所需的串行行为.

  • If you don't want a serial NSOperationQueue, you can use a standard concurrent queue, but just make each operation dependent upon the prior one. You'll achieve the serial behavior you're looking for without using a serial queue.

    例如:

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    NSOperation *operation;
    NSOperation *previousOperation;
    
    operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"Starting #1");
        sleep(3);
        NSLog(@"Finishing #1");
    }];
    [queue addOperation:operation];
    
    previousOperation = operation;
    operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"Starting #2");
        sleep(3);
        NSLog(@"Finishing #2");
    }];
    [operation addDependency:previousOperation];
    [queue addOperation:operation];
    
    previousOperation = operation;
    operation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"Starting #3");
        sleep(3);
        NSLog(@"Finishing #3");
    }];
    [operation addDependency:previousOperation];
    [queue addOperation:operation];
    

  • 您还可以使用dispatch_queue_create创建GCD串行队列.除了使用GCD代替NSOperationQueue.

  • You could also create a GCD serial queue with dispatch_queue_create. It achieves the same thing as the first option, except using GCD instead of NSOperationQueue.

    例如:

    dispatch_queue_t queue = dispatch_queue_create("com.ConnerDouglass.operationtest", 0);
    
    dispatch_async(queue, ^{
        NSLog(@"Starting #1");
        sleep(3);
        NSLog(@"Finishing #1");
    });
    
    dispatch_async(queue, ^{
        NSLog(@"Starting #2");
        sleep(3);
        NSLog(@"Finishing #2");
    });
    
    dispatch_async(queue, ^{
        NSLog(@"Starting #3");
        sleep(3);
        NSLog(@"Finishing #3");
    });
    

  • 这篇关于序列化异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆