简体   繁体   English

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

[英]Add multiple fields to the same recordName row in CloudKit

I am trying to save multiple values to CloudKit (Name, Username, Points). 我正在尝试将多个值保存到CloudKit(名称,用户名,积分)。 However, when I save them, they save separately in 3 different rows, like so: 但是,当我保存它们时,它们分别保存在3个不同的行中,如下所示:

在此处输入图片说明

I want to combine the Name, Username, and Points fields into one row so I can load them together. 我想将“名称”,“用户名”和“积分”字段合并为一行,以便可以将它们一起加载。

Here is my saving code: 这是我的保存代码:

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

Can someone please demonstrate how to save these records together instead of separately? 有人可以演示如何将这些记录一起保存而不是分开保存吗?

Along with that, I currently load them separately when it should be together... Loading code: 除此之外,我目前应该将它们放在一起时分别加载它们... 加载代码:

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

        }
    }
}

I am looking to group my data together in both the saving and loading process. 我希望在保存和加载过程中将数据分组在一起。

PS If it helps anyone understand better, I am trying to make a public leaderboard containing all the users' names or usernames, and their points. PS:如果可以帮助任何人更好地理解,我将尝试制作一个公共排行榜,其中包含所有用户的名称或用户名及其观点。

You're creating 3 separate records and setting one field, a different field, on each one. 您将创建3个单独的记录,并在每个记录上设置一个字段,即另一个字段。 Instead, just create 1 record and set all 3 fields on it. 相反,只需创建1条记录并在其上设置所有3个字段。 Try something like this (I'm not really up to speed on SWIFT, so you may have to play with the syntax a little) 尝试这样的操作(我不太了解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")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM