Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you are trying to do is often called a "data store" singleton, and it's a good design pattern to use. What I do in my applications is have a singleton class named <code>DataStore</code>, you can call it whatever you wish, with a class method:</p> <pre><code>+ (id)sharedStore{ static DataStore *sharedStore = nil; if (!sharedStore) { sharedStore = [[self alloc] init]; } return sharedStore; } </code></pre> <p>Whenever I need access to the resources provided by the datastore, I do:</p> <pre><code>Datastore *ds = [Datastore sharedStore]; </code></pre> <p>To provide access to Core Data, I have a data store method:</p> <pre><code>+ (NSManagedObjectContext*)managedObjectContext{ static NSManagedObjectContext *context = nil; if(context){ return context; } NSPersistentStoreCoordinator *coordinator = nil; NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]; NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; if (!coordinator) { coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:objectModel]; } if(!coordinator){ return nil; } NSString *storePath = [[self documentsDirectoryPath] stringByAppendingPathComponent:@"datastore.sqlite"]; NSURL *storeURL = [NSURL URLWithString:storePath]; NSError *error; if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:NULL error:&amp;error]) { NSLog(@"Database error: %@", error); // if you make changes to your model and a database already exists in the app // you'll get a NSInternalInconsistencyException exception. When the model is updated // the databasefile must be removed. Remove the database here because it's easy. NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtURL:storeURL error:nil]; //try to add the persistant store one more time. If it still fails then abort if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:NULL error:&amp;error]) return nil; } context = [[NSManagedObjectContext alloc] init]; [context setPersistentStoreCoordinator:coordinator]; [context setUndoManager:nil]; return context; } </code></pre> <p>This method returns <code>nil</code> if an <code>NSManagedObjectContext</code> couldn't be created. This way you only need to do:</p> <p><code>NSManagedObjectContext *context = [[DataStore sharedStore] managedObjectContext];</code></p> <p>whenever you need to use Core Data. This can be done once in <code>viewDidLoad</code>. </p> <p>Edit:</p> <p>The <code>managedObjectContext</code> method uses the method below to find the documents directory:</p> <pre><code>+ (NSString *)documentsDirectoryPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *basePath = ([paths count] &gt; 0) ? [paths objectAtIndex:0] : nil; return basePath; } </code></pre>
 

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