100% swift. thread safe, easy writing and reading operation for CoreData.
As you can see, there is three kind of NSManagedObjectContext.
The private managed object context is a background context and it's linked to the persistent store coordinator. Data is written locally hhen the private managed object context save it changes.
It have one child which is the main managed object context.
Child of the private managed object context. Associate to the main thread, it is use for reading operation.
When you need to perform write, update or delete operations, it is a best practice to use a new context used for those operations.
A child managed object context is create with transaction method. When an operation is done, the child is merged into the main managed object context.
//prepare Mellon Store
MellonStore.modelName = "ModelName"
MellonStore.setup() // can be async
// create a fruit
MellonStore.default.transaction { context in
let fruit = try? Fruit.create(in: context)
fruit?.name = name
}
// get all fruits
let fruits = Query(Fruit.self).all()
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate MellonStore into your Xcode project using CocoaPods, it in your Podfile
:
platform :ios, '9.0'
use_frameworks!
source "https://github.com/MellonMellon/Specs.git"
target '<Your Target Name>' do
pod 'MellonStore'
end
Then, run the following command:
$ pod install
transaction method create a background context that you can use for creating, udpating and/or deleting operation.
MellonStore.default.transaction { context in
let fruit = try? Fruit.create(in: context)
fruit?.name = name
}
Or, you can create a new context and use it.
let backgroundContext = MellonStore.default.newBackgroundContext()
let fruit = try? Fruit.create(in: context)
fruit?.name = name
try! backgroundContext.save()
let fruits = Query(Fruit.self).all()
let fruit = Query(Fruit.self).first() // return a single fruit: Fruit
Chain your filter condition using with
let fruits = Query(Fruit.self)
.with("name", equalTo: "banana")
.with("name", like: "banana")
.with("name", existing: true)
.with("name", containing: "ban")
.with("created", lowerThan: Date())
.with("created", greaterThan: Date())
.with("created", lowerThanOrEqual: Date())
.with("name", containedIn: ["banana", "coconut"])
.with("name", startingWith: "ban")
.with("name", endingWith: "na")
.all() // return an array: [Fruit]
let elasticSearch = Query(Fruit.self).elastic()
let total = elasticSearch.totalNumberOfResults
let allFruits: [Fruit] = []
while elasticSearch.canLoadMore {
let fruits = elasticSearch.loadMore()
allFruits.append(fruits)
}
let fruits = Query(Fruit.self).sort("name")
MellonStore.setup(storeName: "another_store")
MellonStore.default.copyStore(for: "new_storage")
MellonStore.deleteStore(for: "storage_identifier")