zwaldowski / AZCoreRecord

Effortless fetching, saving, importing, and ubiquity for Core Data
MIT License
38 stars 1 forks source link

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

Open tonyarnold opened 12 years ago

tonyarnold commented 12 years ago

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.

zwaldowski commented 12 years ago

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.

tonyarnold commented 12 years ago

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.

zwaldowski commented 12 years ago

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.

tonyarnold commented 12 years ago

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.