简单的GCD串行队列示例,例如使用块的FIFO [英] Simple GCD Serial Queue example like FIFO using blocks

查看:108
本文介绍了简单的GCD串行队列示例,例如使用块的FIFO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读Apple文档,了解如何使用串行队列来确保以可预测的顺序执行任务,但现在我感到很困惑。

有些我怎么能连续工作但我仍然是不清楚所以我需要简单的串行示例,我的方法可以串行执行。

I read Apple documentation on how to Use serial queues to ensure that tasks to execute in a predictable order but now i am confused too much.
Some how i am able to work serially but still i am not clear so i need simple serial example for my methods to execute serially.

我将我的功能划分为4个部分,现在希望它们执行Serially

I divided my functionality in to 4 parts and now want them to execute Serially

[self ReadAllImagesFromPhotosLibrary];

[self WriteFewImagestoDirectory];

[self GettingBackAllImagesFromFolder]; 

[self MoveToNextView];


推荐答案

跟进并改进 iCoder 的答案,你可以而且应该做到以下几点。

To follow-up and improve iCoder's answer, you could and should do the following.

dispatch_queue_t serialQueue = dispatch_queue_create("com.unique.name.queue", DISPATCH_QUEUE_SERIAL);

dispatch_async(serialQueue, ^{
        [self ReadAllImagesFromPhotosLibrary];
    }); 
dispatch_async(serialQueue, ^{
         [self WriteFewImagestoDirectory];
});
dispatch_async(serialQueue, ^{
    [self GettingBackAllImagesFromFolder]; 
});
dispatch_async(serialQueue, ^{
    [self MoveToNextView];
});

尽管以上呼叫为异步,但它们将排队并以 DISPATCH_QUEUE_SERIAL 状态串行运行同步 async 之间的区别在于同步,您的代码将暂停并等待以获得阻止答案运行以下代码,因此如果执行时间很长,可能会冻结您的UI。使用 async 时,代码会运行,并且异步返回

Despite the above calls being async, they will be queued and run serially as the DISPATCH_QUEUE_SERIAL states. The difference between sync and async is that with sync, your code will pause and wait for the block answer before running the following code, thus potentially freezing your UI if the execution time is long. Whereas with async, the code runs on and the block is returned asynchronously.

但是,您存储在 DISPATCH_QUEUE_SERIAL 中的任务将等待并按照添加顺序依次执行,这要归功于GCD(Grand Central Dispatch)。

However, the tasks you have stored in the DISPATCH_QUEUE_SERIAL will wait and be executed one after the other in the order they were added, thanks to GCD (Grand Central Dispatch).

这篇关于简单的GCD串行队列示例,例如使用块的FIFO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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