在移动到下一行代码之前等待函数先执行
Wait for function to execute first before moving to next line of code
我在我的应用程序中使用 CloudKit,但在 table 视图中显示数据时遇到问题。在 viewDidLoad()
中,我正在从 CloudKit 数据库中获取数据。
然后在 table 视图函数中,我对行数进行 CKRecord
对象计数。
但数 returns 0 到 table 视图,几秒后 returns 行数。因此 table 视图不显示结果。
override func viewDidLoad() {
super.viewDidLoad()
loadNewData()
}
func loadNewData() {
self.loadData = [CKRecord]()
let publicData = CKContainer.default().publicCloudDatabase
let qry = CKQuery(recordType: "Transactions", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
qry.sortDescriptors = [NSSortDescriptor(key: "Transaction_ID", ascending: true)]
publicData.perform(qry, inZoneWith: nil) { (results, error) in
if let rcds = results {
self.loadData = rcds
}
if error != nil {
self.showAlert(msg: (error?.localizedDescription)!)
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return loadData.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! ViewAllTransactionsTVCell
let pn = loadData[indexPath.row].value(forKey: "Party_Name") as! String
let amt = loadData[indexPath.row].value(forKey: "Amount") as! String
let nrt = loadData[indexPath.row].value(forKey: "Narattions") as! String
let dt = loadData[indexPath.row].value(forKey: "Trans_Date") as! String
cell.partyNameLabel.text = pn
cell.dateLabel.text = dt
cell.narationLabel.text = nrt
cell.amountLabel.text = amt
return cell
}
您不应该等待,而是在调用 perform
完成处理程序时触发数据的重新加载:
publicData.perform(qry, inZoneWith: nil) { (results, error) in
if let rcds = results {
DispatchQueue.main.async {
self.loadData = rcds
self.tableView.reloadData()
}
}
if error != nil {
self.showAlert(msg: (error?.localizedDescription)!)
}
}
请注意,我正在将重新加载进程分派到主队列,因为您不能保证在主线程上有此 运行。正如 the documentation 所说:
Your block must be capable of running on any thread of the app ...
而且因为 UI 更新必须发生在主线程上(并且因为你想同步你对 loadData
的访问),就像上面一样将它分派到主队列。
我在我的应用程序中使用 CloudKit,但在 table 视图中显示数据时遇到问题。在 viewDidLoad()
中,我正在从 CloudKit 数据库中获取数据。
然后在 table 视图函数中,我对行数进行 CKRecord
对象计数。
但数 returns 0 到 table 视图,几秒后 returns 行数。因此 table 视图不显示结果。
override func viewDidLoad() {
super.viewDidLoad()
loadNewData()
}
func loadNewData() {
self.loadData = [CKRecord]()
let publicData = CKContainer.default().publicCloudDatabase
let qry = CKQuery(recordType: "Transactions", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
qry.sortDescriptors = [NSSortDescriptor(key: "Transaction_ID", ascending: true)]
publicData.perform(qry, inZoneWith: nil) { (results, error) in
if let rcds = results {
self.loadData = rcds
}
if error != nil {
self.showAlert(msg: (error?.localizedDescription)!)
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return loadData.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath) as! ViewAllTransactionsTVCell
let pn = loadData[indexPath.row].value(forKey: "Party_Name") as! String
let amt = loadData[indexPath.row].value(forKey: "Amount") as! String
let nrt = loadData[indexPath.row].value(forKey: "Narattions") as! String
let dt = loadData[indexPath.row].value(forKey: "Trans_Date") as! String
cell.partyNameLabel.text = pn
cell.dateLabel.text = dt
cell.narationLabel.text = nrt
cell.amountLabel.text = amt
return cell
}
您不应该等待,而是在调用 perform
完成处理程序时触发数据的重新加载:
publicData.perform(qry, inZoneWith: nil) { (results, error) in
if let rcds = results {
DispatchQueue.main.async {
self.loadData = rcds
self.tableView.reloadData()
}
}
if error != nil {
self.showAlert(msg: (error?.localizedDescription)!)
}
}
请注意,我正在将重新加载进程分派到主队列,因为您不能保证在主线程上有此 运行。正如 the documentation 所说:
Your block must be capable of running on any thread of the app ...
而且因为 UI 更新必须发生在主线程上(并且因为你想同步你对 loadData
的访问),就像上面一样将它分派到主队列。