Skip to content

TL;DR (a.k.a. sample codes)

Eliseo Juan Quintanilla edited this page Jan 3, 2017 · 3 revisions

Setting-up with incremental migration support:

CoreStore.defaultStack = DataStack(
    modelName: "MyStore",
    migrationChain: ["MyStore", "MyStoreV2", "MyStoreV3"]
)

Adding a store:

do {
    try CoreStore.addStorage(
        SQLiteStore(fileName: "MyStore.sqlite"),
        completion: { (result) -> Void in
            // ...
        }
    )
}
catch {
    // ...
}

Starting transactions:

CoreStore.beginAsynchronous { (transaction) -> Void in
    let person = transaction.create(Into(MyPersonEntity))
    person.name = "John Smith"
    person.age = 42

    transaction.commit { (result) -> Void in
        switch result {
            case .Success(let hasChanges): print("success!")
            case .Failure(let error): print(error)
        }
    }
}

Fetching objects:

let people = CoreStore.fetchAll(From(MyPersonEntity))
let people = CoreStore.fetchAll(
    From(MyPersonEntity),
    Where("age > 30"),
    OrderBy(.Ascending("name"), .Descending("age")),
    Tweak { (fetchRequest) -> Void in
        fetchRequest.includesPendingChanges = false
    }
)

Querying values:

let maxAge = CoreStore.queryValue(
    From(MyPersonEntity),
    Select<Int>(.Maximum("age"))
)

But really, there's a reason I wrote this huge Wiki. Read up on the details!

Check out the CoreStoreDemo app project for sample codes as well!

Contents