1
votes

I am using the performBlock: and performBlockAndWait: methods to execute fetch requests on my context on a read only database (it is packaged with my app so never written to).

Am I supposed to be wrapping every NSManagedObject accessor inside performBlockAndWait: as well? - that'd be pretty messy. I'm currently getting crashes whenever CoreData is faulting a one-to-many relationship while CoreData's private queue is doing an execute with performBlock:

Something like:

NSManagedObject* alreadyFetchedObject = ...;
NSArray* alreadyFetchedObject.otherObjects; // Crashes here on main thread (no performBlock wrapped around accessing otherObjects)

.

[context performBlockAndWait:^{
    // Currently executing here on CoreData's own queue
    result = [context executeFetchRequest:fetchRequest error:nil];
}];
1

1 Answers

1
votes

Yes, of course. Maybe with the exception for managed objects which have been associated to a context which is executed on the main thread. But for clarity, I would always wrap accesses into performBlock:. Be careful with performBlockAndWait: - this is error prone for dead locks.

Additionally, when you have statements like this:

NSManagedObject* alreadyFetchedObject = ...;

and access alreadyFetchedObject later, you need to ensure that the corresponding managed object context still exists. Thus, always accessing managed objects with performBlock: or performBlockAndWait: reminds you not to accidentally delete the context ;)