我坚持如何编写CloudKit完成块的想法。我的问题是我正在加载数据,然后将数据放在tableView中。问题是我必须用一个按钮手动刷新tableView,而不是自动将数据加载到它。我需要的是在我的查询结束时重新加载表数据的完成块。以下是我的查询功能:
func loadGroupPage() {
print("should go")
// let pred = NSPredicate(format: "Username = %@", usernameText)
let pred = NSPredicate(format: "groupName = %@", "\(selectedCellName)")
let query = CKQuery(recordType: "Group", predicate: pred)
let operation = CKQueryOperation(query: query)
//operation.resultsLimit = CKQueryOperationMaximumResults
operation.recordFetchedBlock = { (record: CKRecord!) in
operation.qualityOfService = .default
if record != nil{
// setup screen after loading data
self.recordToUpdate = record
if creatingNewPost == true{
print(feed)
//create new post record (modify record)
feed.append(postText)
creatingNewPost = false
self.saveNewPost()
}
self.publicOrPrivate = (record.object(forKey: "PublicOrPrivate") as! String)
if self.publicOrPrivate == "Public"{
// public
//print("you have joined")
}else if self.publicOrPrivate == "Private"{
// private
//print("requestSent")
// self.sendRequest()
self.requests = (record.object(forKey: "Requests") as! Array)
}
if self.groupMembs.contains(username) || members.contains(username){
self.joinBtn.alpha = 0
// in group
}else{
self.joinBtn.alpha = 1
}
if self.leaders.contains(username) && self.publicOrPrivate == "Private"{
// leader
self.requestsTableView.alpha = 1
}else{
self.requestsTableView.alpha = 0
}
if self.groupMembs.contains(username) || self.publicOrPrivate == "Public"{
// leader
feed = (record.object(forKey: "groupFeed") as! Array)
print(feed)
}else{
}
self.groupMembs = (record.object(forKey: "memberUsernames") as! Array)
print(self.groupMembs)
self.numberOfMembersInGroup = self.groupMembs.count
self.groupNameLbl.text = selectedCellName
self.numberOfMembersLbl.text = "\(self.numberOfMembersInGroup) Members"
self.leaders = (record.object(forKey: "leaders") as! Array)
//****************************************************
// this does nothing until I refresh it manually!
self.requestsTableView.reloadData()
self.groupFeedTableView.reloadData()
//****************************************************
//Need completion block so the above two lines will fire directly after data is done loading!
}
}
database.add(operation)
}
正如我在上面的代码中记录的那样,刷新表不会在实际的UI中加载任何内容。有人可以写一个完成块,只是为我重新加载tableView(s)?谢谢!
答案 0 :(得分:1)
<强>更新强>
我使用以下代码添加完成:
operation.queryCompletionBlock = { [unowned self] (cursor, error) in
DispatchQueue.main.async {
if error == nil {
self.tableView.reloadData()
}
}
}
像魅力一样!