didcloud makes CloudKit operations easier and less verbose.
The classes you will persist as resources on CloudKit must conform with the Storable
protocol, as the example shows:
import Foundation
import CloudKit
import didicloud
class Todo: Storable {
/// CloudKit resource name
public class override var reference: String { return "Todo" }
/// Custom atributes
var name: String
var description: String?
/// Storable init
required init(_ record: CKRecord) {
self.name = record["name"] as! String
self.description = record["description"] as? String
super.init(record)
}
/// Custom init
init(name: String, description: String?) {
self.name = name
self.description = description
super.init()
}
}
And a request for the list of objects can be done this way:
Storage.getAll() {
(result: Result<[Todo], Error>) in
switch result {
case.failure(let error):
/// Deal with error
print(error.localizedDescription)
case .success(let todos):
/// Use the returned items
}
}
The creation of a new resource can be done this way:
let newTodo = Todo(name: name, description: description)
Storage.create(newTodo) {
result in
switch result {
case .failure(let error):
/// Deal with error
print(error)
case .success(let todo):
/// Continue...
}
}
The update of a resource can be done this way:
todo.name = "New name"
todo.description = "New description"
Storage.update(todo) {
result in
switch result {
case .failure(let error):
/// Deal with error
print(error)
case .success(let todo):
/// Continue...
}
}
The deletion of a resource can be done this way:
Storage.remove(id) {
result in
switch result {
case.failure(let error):
/// Deal with error
print(error)
case .success(let deletedID):
/// Continue...
}
}
You can specify if the operation occurs in the private or public CloudKit database. It can be done this way:
Storage.getAll(storageType: .publicStorage) {
// ...
}
All CRUD methods accept the storageType
option and the default value is .privateStorage
.
To run the example project, clone the repo, and run pod install
from the Example directory first.
In order to make CloudKit requests you must have an active apple developer enrolment. Also, you need to register your app identifier with CloudKit capabilities and create your object mirrors at CloudKit Dashboard. You can use this tutorial to register the identifier and create CloudKit resources.
didicloud is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'didicloud'
didicloud is available under the MIT license. See the LICENSE file for more info.