Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think, it is <strong>bad idea</strong>. Because it's OOP-ugly. What do I suggest? You should make a singleton class <code>SharedStorage</code> like this:</p> <pre><code>+ (SharedStorage*)sharedStorage { static SharedStorage* _sharedStorage = nil; static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ _sharedStorage = [[self alloc] init]; }); return _sharedStorage; } - (SharedStorage*)init { self = [super init]; if (self) { [self setupPersistentStoreCoordinator]; } return self; } - (NSManagedObjectModel*)__managedObjectModel { if (_managedObjectModel_ == nil) { NSBundle *mainBundle = [NSBundle mainBundle]; NSURL *modelURL = [mainBundle URLForResource:@"yourURL" withExtension:@"momd"]; _managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; } return _managedObjectModel_; } - (void)setupPersistentStoreCoordinator { if (_storeCoordinator != nil) return; NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"YourDB.sqlite"]; NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; NSError *error; _storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self __managedObjectModel]]; if (![_storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } } </code></pre> <p>And everywhere you want to use context you have to create a new <code>NSManagedObjectContext</code> with the same <code>persistentStoreCoordinator</code>:</p> <pre><code>self.context = [NSManagedObjectContext new]; self.context.persistentStoreCoordinator = [[SharedStorage sharedStorage] storeCoordinator]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload