Question

Edited title

I am using Core Data to store some data I collect from my server. In appDelegate's applicationDidBecomeActive, I check if the app needs to download new data (from a version-variable on my server). If it has old information, it downloads the new data.

The problem is, in some of my views, I have tableViews. And these get their data from an array of data extracted from Core Data in viewDidLoad. When opening the app, and the viewDidLoad has already been called, and THEN it updates the data in Core Data, when I then enter the views with tableView, all the rows are wrong. In my case, all the rows shows the same image as the first row, and none of them have any text. I am thinking that the old array has some corrupt data which needs to be reloaded.. As I am writing this I realize that viewDidLoad needs to be called again. Or at least the code in viewDidLoad. I do not want to move it to viewDidAppear or willAppear, because that will mean that this happenes everytime. I thought about force-restarting the process, but I read that this wasn't possible, and that Apple would reject a force quit anyway.

Actually, I kinda just need to know how to programmatically unload all views from AppDelegate, so that they will have to call viewDidLoad again. Or force a viewDidLoad again.

Was it helpful?

Solution

If you cannot use a fetched results controller, you could register for the NSManagedObjectContextObjectsDidChangeNotification:

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(contextChanged:)
    name:NSManagedObjectContextObjectsDidChangeNotification
    object:theManagedObjectContext];

and reload the data only if necessary:

- (void)contextChanged:(NSNotification *)note
{
    // reload your table data
}

Don't forget to remove the observer, e.g. in dealloc:

[[NSNotificationCenter defaultCenter] removeObserver:self]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top