CodableCloudKit 1.0.2

CodableCloudKit 1.0.2

Maintained by Laurent Grondin.



  • By
  • Laurent Grondin

CodableCloudKit Logo

Swift 5.0 Version Platform

CodableCloudKit

CodableCloudKit allows you to easily save and retrieve Codable objects to iCloud Database (CloudKit)

Features

  • ℹī¸ 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)
    }
}

Contributing

Contributions are very welcome 🙌

License

CodableCloudKit
Copyright (c) 2019 CodableCloudKit [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.