0

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.

Sti
  • 8,275
  • 9
  • 62
  • 124
  • You should use a `NSFetchedResultsController` for the table view. A FRC tracks changes to the managed object context automatically. – Martin R Sep 02 '12 at 14:53
  • @MartinR - Yes, I probably should.. But from the very beginning I wrote the code to get data from the NSArray with a fetchRequest in the viewDidLoad. I have five tableviews doing this, until I realized I should go with Core Data. This is the very last bug of my app (currently), and the only thing missing is a "unload all views"-command, or "force viewDidLoad next time"-command. – Sti Sep 02 '12 at 15:02
  • 1
    You could move the code from `viewDidLoad` to a separate method `updateData` method. In `viewWillAppear` you can then check `if (needUpdate) [self updateData];` . - Tampering with the view controllers views does not sound like a good idea. – Martin R Sep 02 '12 at 15:08
  • Sounds like a good idea.. Thanks. Will try to figure out how to set/check the needUpdate. – Sti Sep 02 '12 at 15:10
  • 1
    ... or register for the `NSManagedObjectContextObjectsDidChangeNotification` and reload the data only if that notification is posted. – Martin R Sep 02 '12 at 15:12
  • Yea, I'm pretty new to objective-c, and haven't touched the notification-system yet, so I'll try the first idea first, and read up on notifications if that solution runs slow. – Sti Sep 02 '12 at 15:17

1 Answers1

1

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]
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • What do you mean by "remove the observer"? With the use of ARC I do not use dealloc. Do I still need to remove it elsewhere? – Sti Sep 02 '12 at 15:34
  • 1
    Each `addObserver` needs a matching `removeObserver`. Even with ARC you can have a `dealloc` method for this purpose. – Martin R Sep 02 '12 at 15:38
  • I have never used dealloc as I started with iOS after ARC came. Would it make the same result if I put that line of code in the unload method `viewDidUnload`? – Sti Sep 02 '12 at 15:45
  • 1
    According to http://stackoverflow.com/questions/1768076/are-viewdidunload-and-dealloc-always-called-when-tearing-down-a-uiviewcontroller you cannot be sure that `viewDidUnload` is called before `dealloc`. – Martin R Sep 02 '12 at 15:51
  • Okay, the notification worked great, but I still don't get the whole dealloc thing. Thought I'd just run with it, but I get an error with that last code you provided.. Sure that is correct? It says "No known instance method for selector 'addObserver:'" – Sti Sep 02 '12 at 16:15
  • 1
    My fault, it is `removeObserver`. I have updated the answer. – Martin R Sep 02 '12 at 16:21