1
votes

I was learning core data from apple guide when i saw the "initialize the core data stack code"

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
    NSError *error = nil;
    NSPersistentStoreCoordinator *psc = [[self managedObjectContext] persistentStoreCoordinator]

I dont understand why do you need to get pointer to psc again in line 37 when its already been done in line 27. dosn't a block scope go all the way up to the function it is embedded in?

link for doc:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html#//apple_ref/doc/uid/TP40001075-CH4-SW1

1

1 Answers

3
votes

Between the time the block captured the state and the time the block was executed, either the moc or the psc that needed to be operated on could have changed.

By calling [self managedObjectContext], that race condition is mostly eliminated. Mostly only because it is a concurrent queue and, thus, some other queue might muck with the moc or psc concurrently.

I.e. the code is somewhat hardened against concurrency issues.