I am using CloudKit to store a few arrays, then use the arrays to fill a table. I am reloading the table, but it will not show any data. It should be connected to iCloud because I am still able to add to the database.
Code is shown below:
import UIKit
import CloudKit
var selectedCellName = ""
class explore: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var groupNames = [String]()
var Group = [CKRecord]()
override func viewDidLoad() {
super.viewDidLoad()
loadGroups()
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return groupNames.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "exploreCell")! as UITableViewCell
// let nameCell = leaderboardInfo[indexPath.row].object(forKey: "Name") as! String
// let usernameCell = leaderboardInfo[indexPath.row].object(forKey: "Username") as! String
//let pointsCell = leaderboardInfo[indexPath.row].object(forKey: "Points") as! String
var nameCellLbl = cell.viewWithTag(100) as! UILabel
nameCellLbl.text = groupNames[indexPath.row]
return cell
}
}
Here is the loadGroups()
function which is used to query the database:
func loadGroups(){
print("should go")
// let pred = NSPredicate(format: "Username = %@", usernameText)
let pred = NSPredicate(value: true)
let query = CKQuery(recordType: "Group", predicate: pred)
let operation = CKQueryOperation(query: query)
//operation.resultsLimit = CKQueryOperationMaximumResults
operation.qualityOfService = .default
operation.recordFetchedBlock = { (record: CKRecord!) in
if record != nil{
// need self in front?
groupNames.append(record.object(forKey: "groupName") as! String)
//self.tableView.reloadData()
//print(self.groupNames.count)
}
}
database.add(operation)
//self.tableView.reloadData()
// print(leaderboardInfo.count)
}
This used to work, and if I made changes I made them months ago so I can't remember. Anyway this is the problem in all of my files where I query the database, but the table won't load it.