ios NSOperationQueue,操作全部在添加时运行,不排队 [英] ios NSOperationQueue, operations all run when added and don't queue

查看:178
本文介绍了ios NSOperationQueue,操作全部在添加时运行,不排队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一组当前一起运行的ASINetworkQueues,导致数据库保存时出现竞争条件。我正在尝试创建一个NSOperationQueue,它将对每个子队列进行排队。我目前用一个主方法创建了一个NSOperation,它启动了子队列以开始下载。

So, I have a group of ASINetworkQueues that currently run together resulting in a race condition when it comes to DB saves. I am trying to create an NSOperationQueue that will will queue each of these "sub queues". I currently have created an NSOperation with a main method that kicks off the "sub queue" to start it's download.

我的问题是每次我添加一个子队列到使用addOperation的主队列会立即触发主方法,以便我的子队列同时运行。我的印象是主要方法一次被解雇一次。即仅适用于队列顶部的操作。

My problem is that each time I add a sub queue to the main queue using "addOperation" it fires that "main" method straight away so my sub queues run concurrently. I was under the impression the main method was fired one at a time. i.e. only for the operation at the top of the queue.

也许我在这里遗漏了一些东西,但我似乎无法弄清楚如何阻止其他操作直到第一个完成了。此外,我甚至无法让我的程序进入导致NSOperation的isReady = 0的情况..总是回报是。

Maybe i'm missing something here but I can't seem to figure out how to stall the other operations until the first one has finished. Also, I can't even seem to get my program into a situation that results in isReady = 0 for the NSOperation.. that always gives back yes.

这里是一些代码:

注意:我已将NSOperation队列maxConcurrentOperations设置为1.

NOTE: I have set the NSOperation queue maxConcurrentOperations to 1.

NSOperation主要方法:

NSOperation Main method:

-(void)main {
    [subQueue download];
}

设置队列:

ChapterDownloadOperation *cdo =  [[ChapterDownloadOperation alloc] init];
cdo.chapter = ch;
[chapterDownloadQueue addOperation:cdo];
[cdo release];

如果我添加多个操作,则main方法会在实例中触发它添加到队列中。是否有另一个功能,我应该覆盖并使用该操作准备好摇滚。我有一种感觉,主要的方法是设置操作。

If I add multiple operations the main method fires at the instance it is added to the queue. Is there another function that I should override and use for when that operation is ready to rock. I have a feeling the main method is for setting up the operation.

任何想法都非常感谢。

谢谢这么多。

推荐答案

一旦NSOperationQueue可以平衡额外的处理能力,它就会触发主方法。

NSOperationQueue will fire the main method as soon as it can balance that additional processing power.

要一次将队列限制为一个操作,您可以尝试在排队之前在每个操作之间添加依赖关系:
[B addDependency:A]; // [B main]在A执行完毕后才会被调用

To limit the queue to one Operation at a time, you could try adding dependencies between each operation before you queue them: [B addDependency:A]; //[B main] will not be called until A has finished executing

请注意,如果A被取消,B仍然会运行。

Do note however that if A is cancelled, B will still run.

希望有所帮助!

这篇关于ios NSOperationQueue,操作全部在添加时运行,不排队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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