表将不会加载/显示CloudKit数据 [英] Table Will Not Load/Show CloudKit Data

查看:56
本文介绍了表将不会加载/显示CloudKit数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CloudKit存储一些数组,然后使用这些数组填充表格。我正在重新加载表,但它不会显示任何数据。它应该已连接到iCloud,因为我仍然可以添加到数据库中。

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.

代码如下所示:

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()函数,用于查询数据库:

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.

推荐答案

您要重新加载表视图的时间早于实际查询数据的时间。

You're reloading the table view long before you actually query the data.

将调用删除到 reloadData 中code> viewDidLoad 。

Remove the call to reloadData in viewDidLoad.

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

In loadGroups you need to setup a query completion handler. You need to deal with the possibility that the query completion hander will indicate that there is more data to load.

您应该从查询到的数据中构建一个新数组。在拥有所有数据之前,请勿更新 groupNames 。然后,在主队列上,用查询的结果更新 groupNames ,然后重新加载表视图。

You should build a new array from the queried data. Don't update groupNames until you have all of the data. Then, on the main queue, update groupNames with the queried results and then reload the table view.

一个偶数更好的解决方案是将完成处理程序添加到您的 loadGroups 方法。然后,与其直接 loadGroups 更新 groupNames 并重新加载表视图,它不只是传回包含所有查询数据。然后,调用者可以更新 groupNames 并重新加载表视图,该表视图将其传递给调用的完成块 loadGroups

An even better solution is to add a completion handler to your loadGroups method. Then instead of loadGroups directly updating groupNames and reloading the table view, it simply passes back a new array with all of the query data. Then the caller can update the groupNames and reload the table view it the completion block it passes to the call to 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)
    }
}

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

Then this can be called from your view controller as:

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

Th上面的e是一个粗略的轮廓。这并不意味着可以投入生产。

The above is a rough outline. This is not meant to be production ready.

这篇关于表将不会加载/显示CloudKit数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆