CodableCloudKit
CodableCloudKit allows you to easily save and retrieve Codable objects to iCloud Database (CloudKit)
Features
- [x] ℹ️ Add CodableCloudKit features
Example
The example application is the best way to see CodableCloudKit
in action. Simply open the CodableCloudKit.xcodeproj
and run the Example
scheme.
Installation
CocoaPods
CodableCloudKit is available through CocoaPods. To install
it, simply add the following line to your Podfile:
pod 'CodableCloudKit'
Manually
If you prefer not to use any of the aforementioned dependency managers, you can integrate CodableCloudKit into your project manually. Simply drag the Sources
Folder into your Xcode project.
Usage
Before you start, you have to enable CloudKit in your app.
When you did that, go to your dashboard and create all the record types you need to save.
In this example, we will use User
.
Add a new Field to User
with value
as field name and String
as field type.
All of your objects will be saved as a String.
After that, go in INDEXES, you have to add 2 indexes to your records.
The first one is value
with QUERYABLE
as index type.
The second one is modifiedAt
with SORTABLE
as index type.
Now, we should be good.
Example
Let's say you have a User
model you want to sync to CloudKit. This is what the model would look like:
class User: CodableCloud {
let username: String
}
//OR
class User: CodableCloud /* OR Codable & Cloud */ {
let username: String
enum CodingKeys: String, CodingKey {
case username
}
required init(username: String) {
self.username = username
super.init()
}
required override init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.username = try container.decode(String.self, forKey: .username)
try super.init(from: decoder)
}
}
CodableCloud
is a typealias of Codable & Cloud
.
Save
func saveInCloud(_ database: CKDatabase = CKContainer.default().privateCloudDatabase,
_ completion: ResultCompletion<CKRecord>? = nil)
Save method has 2 parameters : a database with a default value (PrivateCloudDatabase) and an optional completion that returns the CKRecord
. If the object already exists in iCloud, it will update instead of creating a new record.
// The Simplest Way
user.saveInCloud()
// With another Database. In this case, the public database
user.saveInCloud(CKContainer.default().publicCloudDatabase)
// With completion
user.saveInCloud { [weak self] (result: Result<CKRecord>) in
guard let `self` = self else { return }
switch result {
case .success(_):
print("\(user.username) saved in Cloud")
case .failure(let error):
print(error.localizedDescription)
}
}
Retrieve
func retrieveFromCloud(_ database: CKDatabase = CKContainer.default().privateCloudDatabase,
completion: @escaping ResultCompletion<[Self]>)
Retrieve method has 2 parameters : a database with a default value (PrivateCloudDatabase) and an optional completion that returns a [CodableCloud]
.
User.retrieveFromCloud(completion: { [weak self] (result: Result<[User]>) in
guard let `self` = self else { return }
switch result {
case .success(let users):
print("\(users.count) users retrieved from Cloud")
case .failure(let error):
print(error.localizedDescription)
}
})
Remove
func removeFromCloud(_ database: CKDatabase = CKContainer.default().privateCloudDatabase,
_ completion: ResultCompletion<CKRecord.ID?>? = nil)
Retrieve method has 2 parameters : a database with a default value (PrivateCloudDatabase) and an optional completion that returns the CKRecord.ID
as optional.
// The Simplest Way
user.removeFromCloud()
// With another Database. In this case, the public database
user.removeFromCloud(CKContainer.default().publicCloudDatabase)
// With completion
user.removeFromCloud { [weak self] (result: Result<CKRecord.ID?>) in
guard let `self` = self else { return }
switch result {
case .success(_):
print("\(user.username) removed from Cloud")
case .failure(let error):
print(error.localizedDescription)
}
}