0

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.

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.

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.

here's some code:

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

NSOperation Main method:

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

Setting up the queue:

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

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.

Any ideas greatly appreciated.

Thanks so much.

jim
  • 8,670
  • 15
  • 78
  • 149

2 Answers2

2

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

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

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

Hope that helps!

  • Do you mean that you added dependencies to the operations by getting the array of operations in the queue? For dependencies to work properly, you need to add them to each operation BEFORE you add it to the Queue. – Purple Ninja Aug 08 '11 at 22:05
  • 1
    Ok, but operations are created and added to the queue on a button click. So how can I add the first operation as a dependency to the second when the first is added to the queue before another button has been tapped. Any ideas? – jim Aug 09 '11 at 08:23
  • Hmmm then in that case you might want to look into using a serial dispatch queue instead of an operation queue. The serial dispatch queue executes one at a time, in order, first in, first out. There is a lot of helpful info on serial queues in the [Concurrency Programming Guide](http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1) – Purple Ninja Aug 09 '11 at 13:31
  • Cool, thanks. I just converted an Array ingot a Queue and used a global boolean as a semaphore. Appears to be working alright now, i'll have a look at Serial dispatch queues when I get a chance. Thanks so much! – jim Aug 09 '11 at 13:48
0

I have found this example to be very useful http://developer.apple.com/library/ios/#samplecode/ListAdder/Introduction/Intro.html

Praveen R
  • 115
  • 2
  • 6