我正在尝试将多个值保存到 CloudKit(名称、用户名、积分)。但是,当我保存它们时,它们分别保存在 3 个不同的行中,如下所示:
我想将 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()
}
}
}
我希望在保存和加载过程中将我的数据组合在一起。
PS如果它可以帮助任何人更好地理解,我正在尝试制作一个包含所有用户姓名或用户名及其积分的公共排行榜。