Skip to content
John Estropia edited this page Jul 18, 2015 · 3 revisions

The simplest way to initialize CoreStore is to add a default store to the default stack:

do {
    try CoreStore.addSQLiteStoreAndWait()
}
catch {
    // ...
}

This one-liner does the following:

  • Triggers the lazy-initialization of CoreStore.defaultStack with a default DataStack
  • Sets up the stack's NSPersistentStoreCoordinator, the root saving NSManagedObjectContext, and the read-only main NSManagedObjectContext
  • Adds an automigrating SQLite store in the "Application Support" directory with the file name "[App bundle name].sqlite"
  • Creates and returns the NSPersistentStore instance on success, or an NSError on failure

For most cases, this configuration is usable as it is. But for more hardcore settings, refer to this extensive example:

let dataStack = DataStack(modelName: "MyModel") // loads from the "MyModel.xcdatamodeld" file

do {
    // creates an in-memory store with entities from the "Config1" configuration in the .xcdatamodeld file
    let persistentStore = try dataStack.addInMemoryStore(configuration: "Config1") // persistentStore is an NSPersistentStore instance
    print("Successfully created an in-memory store: \(persistentStore)"
}
catch let error as NSError {
    print("Failed creating an in-memory store with error: \(error.description)"
}

do {
    try dataStack.addSQLiteStoreAndWait(
        fileURL: sqliteFileURL, // set the target file URL for the sqlite file
        configuration: "Config2", // use entities from the "Config2" configuration in the .xcdatamodeld file
        automigrating: true, // automatically run lightweight migrations or entity policy migrations when needed
        resetStoreOnMigrationFailure: true)
    print("Successfully created an sqlite store: \(persistentStore)"
}
catch let error as NSError {
    print("Failed creating an sqlite store with error: \(error.description)"
}

CoreStore.defaultStack = dataStack // pass the dataStack to CoreStore for easier access later on

(If you have never heard of "Configurations", you'll find them in your .xcdatamodeld file) xcode configurations screenshot

In our sample above, note that you don't need to do the CoreStore.defaultStack = dataStack line. You can just as well hold a reference to the DataStack like below and call all its instance methods directly:

class MyViewController: UIViewController {
    let dataStack = DataStack(modelName: "MyModel")
    override func viewDidLoad() {
        super.viewDidLoad()
        do {
            try self.dataStack.addSQLiteStoreAndWait()
        }
        catch { // ...
        }
    }
    func methodToBeCalledLaterOn() {
        let objects = self.dataStack.fetchAll(From(MyEntity))
        print(objects)
    }
}

The difference is when you set the stack as the CoreStore.defaultStack, you can call the stack's methods directly from CoreStore itself:

class MyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        do {
            try CoreStore.addSQLiteStoreAndWait()
        }
        catch { // ...
        }
    }
    func methodToBeCalledLaterOn() {
        let objects = CoreStore.fetchAll(From(MyEntity))
        print(objects)
    }
}

Contents

Clone this wiki locally