16 Feb 2013, 21:26

Tim West (4 posts)

Your chapter on core data and threading is very helpful and has helped me get my own import functions running in the background.

But I am confused because in your code examples you add the mail and the import operations to the main queue. So you are still blocking the main queue when you run the operations.

I have had luck with using the following to setup and run things in the background - but I am wondering if there are some implications that I am not aware of that will make this fail:

- (NSOperationQueue *) dataProcessQueue {
    if (!_dataProcessQueue) {
        _dataProcessQueue = [[NSOperationQueue alloc] init];
        _dataProcessQueue.name = @"Data Processing and Import Queue";
        _dataProcessQueue.maxConcurrentOperationCount = 1;
    return _dataProcessQueue;


My import takes about 30 seconds to create 700 objects so I dont want to block the main queue. To let the main queue update the UI as data is getting imported I issue a save from my import operation every 40 entries.


18 Feb 2013, 16:47

Marcus S. Zarra (245 posts)

The @-[NSOperationQueue mainQueue]@ can be a bit confusing. I always explain the @NSOperationQueue@ as the manager. If you have @NSOperation@ instances with @-isConcurrent@ set to @NO@ then they will run on the thread that created the @NSOperationQueue@ which in the case of my examples would be the main queue.

However if you are using @NSOperation@ instances with @-isConcurrent@ set to @YES@ (the default) then the @NSOperationQueue@ will spawn a new thread for each @NSOperation@ up to the maximum number of threads that it is configured to support. None of the @NSOperation@ instances will run on the thread that created the @NSOperationQueue@ instance.

That is why I can use that convenience method and why it is there.

19 Feb 2013, 04:10

Tim West (4 posts)

Thanks for the response - that may work well on the mac but on the iPhone I get:

DLog(@”Number of threads on mainQueue %d”, [[NSOperationQueue mainQueue] maxConcurrentOperationCount]);

Number of threads on mainQueue 1

So calling on the main queue does block the UI in my hands. I tested both on iPhone 4S and 5 to see if it was phone dependent but I get the same result on both.

Also, having your own operation queue allow you to call cancelAllOperations on the queue if you want to cancel all the ongoing import or data processing. I dont know if calling cancelAllOperations on the main queue would be such a good idea.

21 Feb 2013, 20:45

Marcus S. Zarra (245 posts)

I explored this a bit further based on this conversation and my assumption was incorrect. It does seem that @+[NSOperationQueue mainQueue]@ gives you a special case @NSOperationQueue@ that will only execute code on the main queue and thereby ignoring/breaking the contract.

I will update the same code in the projects to account for that clarification.

14 Mar 2014, 15:58

Bram Plessers (1 post)

I think the source code of the book has not been updated.
The code from the book stil blocks the UI when trying to update the UI while importing a lot of data. Using Tim’s method everything runs as expected.

The folder of the book was created on Feb 20 2013.
A day before Marcus’ last response in this thread.

  You must be logged in to comment