Question

I have a question concerning iOS. I couldn't find an answer to my question online but maybe one of you know the answer. Im working on an app which pulls data from an online database. A NSObject Class is called by a timer in the background of the app. I want it to tell one of my view controller to reload its tableview if it finds new data. Like it is supposed to check for data not caring in which view controller im in. But when I'm in the specific one with my table view it should reload the table view if it found new data. I've tried to create an instance of the tableviewcontroller in my NSObject and call the reload table view function front there, but that doesn't work :/ I apologise for my poor explanation, I'm not a native speaker.

Thank you very much :)

Was it helpful?

Solution

You can use two approaches:

  1. post a NSNotification in your class that gets more data and observes this notification in your class that the TableView is initialized.
  2. make a delegate approach between the two classes and fire a method when you have the new data.

I would go with the first option:

a. in your data loading class:

NSNotification *notification = [NSNotification notificationWithName:@"newDataFetched" object:anyObject];
[[NSNotificationCenter defaultCenter] postNotification:notification];

b. in your listener class:

[[NSNotificationCenter defaultCenter]
        addObserver:self
           selector:@selector(aMethodToReloadTheTableView:)
               name:@"newDataFetched"
             object:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top