将多个字段添加到 CloudKit 中的同一 recordName 行

Add multiple fields to the same recordName row in CloudKit

我正在尝试将多个值保存到 CloudKit(名称、用户名、积分)。但是,当我保存它们时,它们分别保存在 3 个不同的行中,如下所示:

我想将“姓名”、“用户名”和“积分”字段合并到一行中,以便我可以将它们一起加载。

这是我的保存代码:

func saveToCloud(){
    let newName = CKRecord(recordType: "LeaderboardInfo")
    newName.setValue(firstName, forKey: "Name")
        database.save(newName) { (record, error) in
        guard record != nil else { return }
        print("saved record")
    }
    let newUsername = CKRecord(recordType: "LeaderboardInfo")
    newUsername.setValue(username, forKey: "Username")

    database.save(newUsername) { (record, error) in
        guard record != nil else { return }
        print("saved record")
    }
    let pointsCount = CKRecord(recordType: "LeaderboardInfo")
    pointsEarned = Int(arc4random_uniform(UInt32(50)))
    pointsCount.setValue(pointsEarned, forKey: "Points")

    database.save(pointsCount) { (record, error) in
        guard record != nil else { return }
        print("saved record")
    }
}

有人可以演示如何将这些记录一起保存而不是单独保存吗?

除此之外,我目前在应该在一起的时候分别加载它们...加载代码:

 func queryDatabase() {
    // Name
    let query = CKQuery(recordType: "LeaderboardInfo", predicate: NSPredicate(value: true))
    database.perform(query, inZoneWith: nil) { (records, _) in
        guard let records = records else { return }
        let sortedRecords = records.sorted(by: { [=12=].creationDate! > .creationDate! })
        self.names = sortedRecords
        DispatchQueue.main.async {
            //  self.tableView.refreshControl?.endRefreshing()
              self.tableView.reloadData()
        }
    }
    // Username
    let query2 = CKQuery(recordType: "LeaderboardInfo", predicate: NSPredicate(value: true))
    database.perform(query2, inZoneWith: nil) { (records, _) in
        guard let records = records else { return }
        let sortedRecords = records.sorted(by: { [=12=].creationDate! > .creationDate! })
        self.usernames = sortedRecords
        DispatchQueue.main.async {
            //  self.tableView.refreshControl?.endRefreshing()
              self.tableView.reloadData()
        }
    }
    // Points
    let query3 = CKQuery(recordType: "LeaderboardInfo", predicate: NSPredicate(value: true))
    database.perform(query3, inZoneWith: nil) { (records, _) in
        guard let records = records else { return }
        let sortedRecords = records.sorted(by: { [=12=].creationDate! > .creationDate! })
        self.points = sortedRecords
        DispatchQueue.main.async {
            //  self.tableView.refreshControl?.endRefreshing()
              self.tableView.reloadData()

        }
    }
}

我希望在保存和加载过程中将我的数据分组在一起。

P.S。如果它可以帮助任何人更好地理解,我正在尝试制作一个 public 排行榜,其中包含所有用户的姓名或用户名,以及他们的积分。

您正在创建 3 条单独的记录并在每条记录上设置一个字段,一个不同的字段。相反,只需创建 1 条记录并在其上设置所有 3 个字段。试试这样的东西(我真的跟不上 SWIFT,所以你可能需要稍微玩一下语法)

func saveToCloud()
{
    let newRecord = CKRecord(recordType: "LeaderboardInfo")

    newRecord.setValue(firstName, forKey: "Name")
    newRecord.setValue(username, forKey: "Username")

    pointsEarned = Int(arc4random_uniform(UInt32(50)))
    newRecord.setValue(pointsEarned, forKey: "Points")

    database.save(newRecord) { (record, error) in
        guard record != nil else { return }
        print("saved record")
    }
}