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

查看:76
本文介绍了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"向主队列添加子队列时,它会立即触发main"方法,因此我的子队列并发运行.我的印象是一次触发一个主要方法.即仅用于队列顶部的操作.

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 的情况...总是返回 yes.

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 Main 方法:

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 将在能够平衡额外的处理能力后立即触发 main 方法.

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天全站免登陆