使用CloudKit完成performQuery操作后重新加载我的表。问题是使用来自云的数据重新加载表。我的数据很好,没有任何问题。
我发现这个帖子我认为会解决它,但我不知道何时调用它以及如何调用它。
Wait For Asynchronous Operation To Complete in Swift
- 代码
import UIKit
import CloudKit
class DownloadTableViewController: UITableViewController {
var rooms: [String] = []
var appliance: [String] = []
let container = CKContainer.defaultContainer()
var publicDatabase: CKDatabase?
var currentRecord: CKRecord?
let publicDB = CKContainer.defaultContainer().privateCloudDatabase
var receivedString = myHouse.houseData
override func viewDidLoad() {
super.viewDidLoad()
rooms.removeAll()
appliance.removeAll()
reloadMyData()
//performQuery()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func viewDidAppear(animated: Bool) {
rooms.removeAll()
appliance.removeAll()
reloadMyData()
print("Calling query")
performQuery()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
print("Received memory warning")
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if rooms.count == 0 {
return 1
}
else {
return rooms.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("downloadTableCell", forIndexPath: indexPath)
as! DownloadTableViewCell
print(" rooms: \(rooms.count)")
// Configure the cell...
if rooms.count == 0 {
cell.downloadLabel.text = "Waiting for Cloud Data....."
}
else {
cell.downloadLabel.text = rooms[indexPath.row] + " - " + appliance[indexPath.row]
}
return cell
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
func performQuery() {
//let tempCloudID = "B16AEB76-AA36-491A-B3C7-775602466ECB"
//application(application: UIApplication!, performFetchWithCompletionHandler completionHandler: ((UIBackgroundFetchResult) -> Void)!)
// NOT SURE HOW TO CALL THE ABOVE LINE OR WHERE TO CALL IT
let tempCloudID = myCurrentHouse[5]
let predicate = NSPredicate(format: "CloudID = %@", tempCloudID)
//let sort = NSSortDescriptor(key: "Room", ascending: true)
let query = CKQuery(recordType: "ImagesSaved", predicate: predicate)
query.sortDescriptors = [NSSortDescriptor(key: "Room", ascending: true)]
publicDB.performQuery(query, inZoneWithID: nil , completionHandler: ({ results, error in
if(error != nil) {
dispatch_async(dispatch_get_main_queue()) {
self.notifyUser("Cloud Access Error", message: error!.localizedDescription)
print("Error")
self.rooms.removeAll()
self.appliance.removeAll()
self.rooms.append("Cloud Error")
self.appliance.append("Connected?")
//self.reloadMyData()
}
}
else {
if results!.count > 0 {
self.rooms.removeAll()
self.appliance.removeAll()
for record in results! {
self.rooms.append(record.objectForKey("Room") as! String)
self.appliance.append(record.objectForKey("Appliance") as! String)
print("I have results from query")
}
self.reloadMyData()
}
else {
print("query returned with no results")
self.rooms.removeAll()
self.appliance.removeAll()
self.rooms.append("No Data")
self.appliance.append(" ")
self.reloadMyData()
}
}
}))
//self.reloadMyData()
}
func notifyUser(title: String, message: String) -> Void {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
alert.addAction(cancelAction)
self.presentViewController(alert, animated: true, completion: nil)
}
func reloadMyData() {
print("Reloading data \(self.rooms.count)")
self.tableView.reloadData()
}
}