Skip to content
This repository was archived by the owner on May 8, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions DCTCoreDataStack/DCTCoreDataStack.m
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,22 @@ - (NSManagedObjectContext *)rootContext {

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (_persistentStoreCoordinator == nil)
[self _loadPersistentStoreCoordinator];
if (_persistentStoreCoordinator != nil)
return _persistentStoreCoordinator;

// This forces all threads that reach this
// code to be processed in an ordered manner on the main thread. The first
// one will initialize the data, and the rest will just return with that
// data. However, it ensures the creation is not attempted multiple times.
// from http://stackoverflow.com/questions/10388724/random-exc-bad-access-with-persistentstorecoordinator
if (![NSThread currentThread].isMainThread) {
dispatch_sync(dispatch_get_main_queue(), ^{
(void)[self persistentStoreCoordinator];
});
return _persistentStoreCoordinator;
}

[self _loadPersistentStoreCoordinator];

return _persistentStoreCoordinator;
}
Expand Down