From 1ebf7bb9a30b8449ec4d9592e00a50ba42b0d5d8 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Wed, 3 Jun 2015 01:38:18 +0900 Subject: [PATCH] Created Setting up (markdown) --- Setting-up.md | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Setting-up.md diff --git a/Setting-up.md b/Setting-up.md new file mode 100644 index 0000000..35729e6 --- /dev/null +++ b/Setting-up.md @@ -0,0 +1,71 @@ +The simplest way to initialize CoreStore is to add a default store to the default stack: +```swift +CoreStore.addSQLiteStore() +``` +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: +```swift +let dataStack = DataStack(modelName: "MyModel") // loads from the "MyModel.xcdatamodeld" file + +switch dataStack.addInMemoryStore(configuration: "Config1") { // creates an in-memory store with entities from the "Config1" configuration in the .xcdatamodeld file +case .Success(let persistentStore): // persistentStore is an NSPersistentStore instance + println("Successfully created an in-memory store: \(persistentStore)" +case .Failure(let error): // error is an NSError instance + println("Failed creating an in-memory store with error: \(error.description)" +} + +switch dataStack.addSQLiteStore( + 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) { // delete and recreate the sqlite file when migration conflicts occur (useful when debugging) +case .Success(let persistentStore): // persistentStore is an NSPersistentStore instance + println("Successfully created an sqlite store: \(persistentStore)" +case .Failure(let error): // error is an NSError instance + println("Failed creating an sqlite store with error: \(error.description)" +} + +CoreStore.defaultStack = dataStack // pass the dataStack to CoreStore for easier access later on +``` + +Note that you dont need to do the `CoreStore.defaultStack = dataStack` line. You can just as well hold a stack like below and call all methods directly from the `DataStack` instance: +```swift +class MyViewController: UIViewController { + let dataStack = DataStack(modelName: "MyModel") + override func viewDidLoad() { + super.viewDidLoad() + self.dataStack.addSQLiteStore() + } + func methodToBeCalledLaterOn() { + let objects = self.dataStack.fetchAll(From(MyEntity)) + println(objects) + } +} +``` +The difference is when you set the stack as the `CoreStore.defaultStack`, you can call the stack's methods directly from `CoreStore` itself: +```swift +class MyViewController: UIViewController { + override func viewDidLoad() { + super.viewDidLoad() + CoreStore.addSQLiteStore() + } + func methodToBeCalledLaterOn() { + let objects = CoreStore.fetchAll(From(MyEntity)) + println(objects) + } +} +``` + + +## Contents +- [[Architecture]] +- [[Setting up]] +- [[Saving and processing transactions]] +- [[Fetching and querying]] +- [[Logging and error handling]] +- [[Observing changes and notifications]] \ No newline at end of file