CodableCloudKit 1.0.2

CodableCloudKit 1.0.2

Laurent Grondin 维护。



  • 作者
  • Laurent Grondin

CodableCloudKit Logo

Swift 5.0 Version Platform

CodableCloudKit

CodableCloudKit 允许您轻松地将 Codable 对象保存和检索到 iCloud 数据库 (CloudKit)

功能

  • ℹ️添加 CodableCloudKit 功能

示例

示例应用程序是最好的方式来看见 CodableCloudKit 的应用。只需要打开 CodableCloudKit.xcodeproj 并运行 Example 方案。

安装

CocoaPods

CodableCloudKit 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行

pod 'CodableCloudKit'

手动

如果您不想使用上述任何依赖管理器,您可以手动将CodableCloudKit集成到您的项目中。只需将Sources文件夹拖到Xcode项目中即可。

使用方法

开始之前,您必须在您的应用程序中启用CloudKit

完成上述步骤后,转到您的仪表板并创建所有需要保存的记录类型。在本例中,我们将使用User。在User中添加一个新的字段,字段名为value,字段类型为String。所有对象都将保存为字符串。

之后,进入索引,您必须在记录中添加2个索引。第一个是具有QUERYABLE索引类型的value。第二个是具有SORTABLE索引类型的modifiedAt

现在应该没问题了。

示例

假设您有一个希望同步到CloudKit的User模型。该模型看起来可能是这样的

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)
    }
}

CodableCloudCodable & Cloud的同义词。

保存

func saveInCloud(_ database: CKDatabase = CKContainer.default().privateCloudDatabase, 
                 _ completion: ResultCompletion<CKRecord>? = nil)

保存方法有两个参数:一个数据库(默认值为PrivateCloudDatabase)和一个可选的完成返回一个CKRecord的完成。如果对象已存在于iCloud中,它将更新,而不是创建新的记录。

// 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)
    }
}

检索

func retrieveFromCloud(_ database: CKDatabase = CKContainer.default().privateCloudDatabase, 
                       completion: @escaping ResultCompletion<[Self]>)

检索方法有两个参数:一个数据库(默认值为PrivateCloudDatabase)和一个可选的完成返回一个[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)
    }
})

删除

func removeFromCloud(_ database: CKDatabase = CKContainer.default().privateCloudDatabase,
                     _ completion: ResultCompletion<CKRecord.ID?>? = nil)

检索方法有2个参数:一个默认值为(PrivateCloudDatabase)的数据库和一个可选的完成,它返回可选的CKRecord.ID

// 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)
    }
}

贡献

欢迎贡献🙌

许可协议

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.