Updated Setting up (markdown)

John Estropia
2015-07-18 23:16:52 +09:00
parent e7abbdadb8
commit 2dd38d7ec1

@@ -1,6 +1,11 @@
The simplest way to initialize CoreStore is to add a default store to the default stack: The simplest way to initialize CoreStore is to add a default store to the default stack:
```swift ```swift
CoreStore.addSQLiteStore() do {
try CoreStore.addSQLiteStoreAndWait()
}
catch {
// ...
}
``` ```
This one-liner does the following: This one-liner does the following:
- Triggers the lazy-initialization of `CoreStore.defaultStack` with a default `DataStack` - Triggers the lazy-initialization of `CoreStore.defaultStack` with a default `DataStack`
@@ -12,38 +17,48 @@ For most cases, this configuration is usable as it is. But for more hardcore set
```swift ```swift
let dataStack = DataStack(modelName: "MyModel") // loads from the "MyModel.xcdatamodeld" file 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 do {
case .Success(let persistentStore): // persistentStore is an NSPersistentStore instance // creates an in-memory store with entities from the "Config1" configuration in the .xcdatamodeld file
println("Successfully created an in-memory store: \(persistentStore)" let persistentStore = try dataStack.addInMemoryStore(configuration: "Config1") // persistentStore is an NSPersistentStore instance
case .Failure(let error): // error is an NSError instance print("Successfully created an in-memory store: \(persistentStore)"
println("Failed creating an in-memory store with error: \(error.description)" }
catch let error as NSError {
print("Failed creating an in-memory store with error: \(error.description)"
} }
switch dataStack.addSQLiteStore( do {
fileURL: sqliteFileURL, // set the target file URL for the sqlite file try dataStack.addSQLiteStoreAndWait(
configuration: "Config2", // use entities from the "Config2" configuration in the .xcdatamodeld file fileURL: sqliteFileURL, // set the target file URL for the sqlite file
automigrating: true, // automatically run lightweight migrations or entity policy migrations when needed configuration: "Config2", // use entities from the "Config2" configuration in the .xcdatamodeld file
resetStoreOnMigrationFailure: true) { // delete and recreate the sqlite file when migration conflicts occur (useful when debugging) automigrating: true, // automatically run lightweight migrations or entity policy migrations when needed
case .Success(let persistentStore): // persistentStore is an NSPersistentStore instance resetStoreOnMigrationFailure: true)
println("Successfully created an sqlite store: \(persistentStore)" print("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)" 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 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: (If you have never heard of "Configurations", you'll find them in your *.xcdatamodeld* file)
<img src="https://cloud.githubusercontent.com/assets/3029684/8333192/e52cfaac-1acc-11e5-9902-08724f9f1324.png" alt="xcode configurations screenshot" height=212 />
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:
```swift ```swift
class MyViewController: UIViewController { class MyViewController: UIViewController {
let dataStack = DataStack(modelName: "MyModel") let dataStack = DataStack(modelName: "MyModel")
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
self.dataStack.addSQLiteStore() do {
try self.dataStack.addSQLiteStoreAndWait()
}
catch { // ...
}
} }
func methodToBeCalledLaterOn() { func methodToBeCalledLaterOn() {
let objects = self.dataStack.fetchAll(From(MyEntity)) let objects = self.dataStack.fetchAll(From(MyEntity))
println(objects) print(objects)
} }
} }
``` ```
@@ -52,11 +67,15 @@ The difference is when you set the stack as the `CoreStore.defaultStack`, you ca
class MyViewController: UIViewController { class MyViewController: UIViewController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
CoreStore.addSQLiteStore() do {
try CoreStore.addSQLiteStoreAndWait()
}
catch { // ...
}
} }
func methodToBeCalledLaterOn() { func methodToBeCalledLaterOn() {
let objects = CoreStore.fetchAll(From(MyEntity)) let objects = CoreStore.fetchAll(From(MyEntity))
println(objects) print(objects)
} }
} }
``` ```