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

时间:2017-08-03 14:12:57

标签: swift cloudkit ckrecord ckquery recordtype

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

enter image description here

我想将Name,Username和Points字段组合成一行,以便我可以将它们一起加载。

以下是保存代码:

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: { $0.creationDate! > $1.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: { $0.creationDate! > $1.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: { $0.creationDate! > $1.creationDate! })
        self.points = sortedRecords
        DispatchQueue.main.async {
            //  self.tableView.refreshControl?.endRefreshing()
              self.tableView.reloadData()

        }
    }
}

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

P.S。如果它有助于任何人更好地理解,我试图制作一个包含所有用户的公共排行榜'名称或用户名及其要点。

1 个答案:

答案 0 :(得分:1)

您正在创建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")
    }
}