Table 不会 Load/Show CloudKit 数据

Table Will Not Load/Show CloudKit Data

我是用CloudKit存几个数组,然后用数组补一个table。我正在重新加载 table,但它不会显示任何数据。它应该连接到 iCloud,因为我仍然可以添加到数据库。

代码如下:

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

这里是用来查询数据库的loadGroups()函数:

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

这曾经有效,如果我做了更改,我是几个月前做的,所以我不记得了。无论如何,这是我查询数据库的所有文件中的问题,但 table 不会加载它。

您在实际查询数据之前很久就重新加载了 table 视图。

删除 viewDidLoad 中对 reloadData 的调用。

loadGroups 中,您需要设置查询完成处理程序。您需要处理查询完成处理程序指示有更多数据要加载的可能性。

您应该根据查询的数据构建一个新数组。在您拥有所有数据之前不要更新 groupNames。然后,在主队列上,使用查询结果更新 groupNames,然后重新加载 table 视图。

一个更好的解决方案是向您的 loadGroups 方法添加一个完成处理程序。然后,不是 loadGroups 直接更新 groupNames 并重新加载 table 视图,而是简单地传回一个包含所有查询数据的新数组。然后调用者可以更新 groupNames 并重新加载 table 查看它传递给调用 loadGroups.

的完成块
func loadGroups(completion: ((_ groups: [String]) -> Void)) {
    var names = [String]()

    // setup operation
    ...
    operation.recordFetchedBlock = { (record: CKRecord!) in
        if record != nil {
            names.append(record.object(forKey: "groupName") as! String)
        }
    }

    operation.queryCompletionBlock = { (cursor, error) in {
        // properly handle cursor and error

        completion(names)
    }
}

然后可以从您的视图控制器调用它:

loadGroups() { (names) in
    DispatchQueue.main.async {
        groupNames = names
        tableView.reloadData()
    }
}

以上是大概的轮廓。这并不意味着生产就绪。