I am trying to save multiple values to CloudKit (Name, Username, Points). However, when I save them, they save separately in 3 different rows, like so:
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.
P.S. If it helps anyone understand better, I am trying to make a public leaderboard containing all the users' names or usernames, and their points.
You're creating 3 separate records and setting one field, a different field, on each one. Instead, just create 1 record and set all 3 fields on it. Try something like this (I'm not really up to speed on SWIFT, so you may have to play with the syntax a little)