mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-16 14:06:53 +01:00
Updated Setting up (markdown)
@@ -1,6 +1,11 @@
|
||||
The simplest way to initialize CoreStore is to add a default store to the default stack:
|
||||
```swift
|
||||
CoreStore.addSQLiteStore()
|
||||
do {
|
||||
try CoreStore.addSQLiteStoreAndWait()
|
||||
}
|
||||
catch {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
This one-liner does the following:
|
||||
- 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
|
||||
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)"
|
||||
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)"
|
||||
}
|
||||
|
||||
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)"
|
||||
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
|
||||
```
|
||||
|
||||
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
|
||||
class MyViewController: UIViewController {
|
||||
let dataStack = DataStack(modelName: "MyModel")
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
self.dataStack.addSQLiteStore()
|
||||
do {
|
||||
try self.dataStack.addSQLiteStoreAndWait()
|
||||
}
|
||||
catch { // ...
|
||||
}
|
||||
}
|
||||
func methodToBeCalledLaterOn() {
|
||||
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 {
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
CoreStore.addSQLiteStore()
|
||||
do {
|
||||
try CoreStore.addSQLiteStoreAndWait()
|
||||
}
|
||||
catch { // ...
|
||||
}
|
||||
}
|
||||
func methodToBeCalledLaterOn() {
|
||||
let objects = CoreStore.fetchAll(From(MyEntity))
|
||||
println(objects)
|
||||
print(objects)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user