Search code examples
iosswiftuitableviewcloudkit

Can't reload TableView


I have 2 controllers: first is showing all records in db, second allows to add new record. Both of them connected with NavigationController. If I'll add new record and press "Back" button, first controller won't show new data (because of NavigationController, i guess). So, I've tried to put tableView.reloadData() in viewDidAppear(). It won't help. Then I've tried to reload tableView with NSNotificationCenter (method from this thread), but it still doesn't show new data. Here is my code:

func saveRecord() {
   //Creating cloud record
   let container = CKContainer.defaultContainer()
   let privateDatabase = container.privateCloudDatabase
   privateDatabase.saveRecord(cloudRecord, completionHandler: { (record, error) -> Void in
      if (error != nil) {
         print(error)
      }
      NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
         //creating core data record
         do {
            try managedContext.save()
         } catch let error as NSError {
            print("Could not save \(error), \(error.userInfo)")
         }
         NSNotificationCenter.defaultCenter().postNotificationName("reloadHistoryController", object: nil)
      })
   })
}

override func viewDidLoad() {
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadHistoryController:", name:"reloadHistoryController", object: nil)
}

func reloadHistoryController(notification: NSNotification){
    self.tableView.reloadData()
}

Update

dispatch_async(dispatch_get_main_queue(),{
        self.tableView.reloadData()
})

Doesn't help me too.


Solution

  • Please check your no. of rows count is same both time i.e. when you load firstviewcontroller and after save new record and press back button. I think your array which hold your data not initialized with new data you added. Please try to initialize your array data in viewWillAppear() method and then reload your tableview.