mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-18 15:09:47 +02:00
Created TL;DR (a.k.a. sample codes) (markdown)
74
TL;DR-(a.k.a.-sample-codes).md
Normal file
74
TL;DR-(a.k.a.-sample-codes).md
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
Setting-up with incremental migration support:
|
||||||
|
```swift
|
||||||
|
CoreStore.defaultStack = DataStack(
|
||||||
|
modelName: "MyStore",
|
||||||
|
migrationChain: ["MyStore", "MyStoreV2", "MyStoreV3"]
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Adding a store:
|
||||||
|
```swift
|
||||||
|
do {
|
||||||
|
try CoreStore.addSQLiteStore(
|
||||||
|
fileName: "MyStore.sqlite",
|
||||||
|
completion: { (result) -> Void in
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Starting transactions:
|
||||||
|
```swift
|
||||||
|
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:
|
||||||
|
```swift
|
||||||
|
let people = CoreStore.fetchAll(From(MyPersonEntity))
|
||||||
|
```
|
||||||
|
```swift
|
||||||
|
let people = CoreStore.fetchAll(
|
||||||
|
From(MyPersonEntity),
|
||||||
|
Where("age > 30"),
|
||||||
|
OrderBy(.Ascending("name"), .Descending("age")),
|
||||||
|
Tweak { (fetchRequest) -> Void in
|
||||||
|
fetchRequest.includesPendingChanges = false
|
||||||
|
}
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
Querying values:
|
||||||
|
```swift
|
||||||
|
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
|
||||||
|
- [[Architecture]]
|
||||||
|
- [[Setting up]]
|
||||||
|
- [[Saving and processing transactions]]
|
||||||
|
- [[Fetching and querying]]
|
||||||
|
- [[Logging and error handling]]
|
||||||
|
- [[Observing changes and notifications]]
|
||||||
Reference in New Issue
Block a user