gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:24:19 26 4
gpt4 key购买 nike

我正在尝试将多个值保存到 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()

}
}
}

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

附言如果它可以帮助任何人更好地理解,我正在尝试制作一个包含所有用户姓名或用户名及其积分的公共(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")
}
}

关于swift - 将多个字段添加到 CloudKit 中的同一 recordName 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45486767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com