AZCoreRecord icon indicating copy to clipboard operation
AZCoreRecord copied to clipboard

Can only use -performBlock: on an NSManagedObjectContext that was created with a queue

Open tonyarnold opened this issue 13 years ago • 4 comments

I'm seeing the following error when trying to use - (void) saveDataInBackgroundWithBlock: (void (^)(NSManagedObjectContext *)) block completion: (void (^)(void)) callback in my code. It looks like the -newChildContext method has a concurrency type of NSConfinementConcurrencyType, which means that you can't use -performBlock: with it.

Changing the concurrency type to NSPrivateQueueConcurrencyType fixes the problem.

tonyarnold avatar Aug 23 '12 21:08 tonyarnold

Oh, jeez, it looks like all my GitHub issues were filed under a spam filter. Whoops.

Uh, I suppose we need to have some different logic in saveDataInBackground for different confinement types. We definitely can't spin up a new private queue along with every context.

zwaldowski avatar Aug 27 '12 02:08 zwaldowski

I thought that spinning up a new context was really lightweight? I recall something being said about it being pretty costless at WWDC a couple of years ago, but I may be mistaken.

tonyarnold avatar Aug 27 '12 02:08 tonyarnold

Contexts themselves are super quick to spin up, but with NSPrivateQueueConcurrencyType spins up a whole private queue, which not only take time to initialize but use kernel-level GCD features which, although objectively fast, can be quite slow for our purposes. Similarly, the NSMainQueueConcurrencyType (which should be rarely used, but I'm using it for an example) attaches itself to the main queue and dispatches against that. The confinement type opts out of all that and follows the 4.0-and-below approach, you make sure it only gets accessed by one thread at a time, which is technically what AZCR does with its usage of child contexts.

zwaldowski avatar Aug 27 '12 02:08 zwaldowski

Righto - then the save methods need a bit of attention ;) If I get some time later today I'll submit a pull request to fix it.

tonyarnold avatar Aug 27 '12 03:08 tonyarnold