0

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.

Community
  • 1
  • 1
dsk1306
  • 129
  • 1
  • 9
  • breakpoint on the function cellForRowAtIndexPath, and see when/if its called when updating the tableView. – Ulysses Mar 05 '16 at 10:51
  • @UlyssesR it isn't calling. I can print some stuff in console through the Notification, but it doesn't want to work with my table at all – dsk1306 Mar 05 '16 at 10:57
  • Have you set delegate of tableview ? – Mahendra Mar 05 '16 at 10:59
  • @UlyssesR i also have another Notification with different name and selector, but with some functions in it and it works ok – dsk1306 Mar 05 '16 at 10:59
  • @MGP it was created with storyboard (TableViewController) and all delegates looks ok – dsk1306 Mar 05 '16 at 11:00

1 Answers1

3

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.

Rohit Khandelwal
  • 1,778
  • 15
  • 23