Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correctly use of NSFetchedResultsController cache

I am using a NSFetchedResultsController for dealing with a UITableView and everything works fine and I found it very powerful, especially in conjunction with the results delegate. For drilling down the table I am reusing the same controller class, which get instantiated with parameters by clicking on a cell row, the controller is then pushed to the UINavigationController and another table view is built, again with a NSFetchedResultsController.

At this point, every controller has its own cache, and the cache name is derived with a unique identifier [NSString stringWithFormat"cacheName_%@",uniqueStringForCell], and in the end I can obviously have many cache.

Now the questions.

1) Having many cache can be a problem ?

2) When are we supposed to use a cache ?

3) In case of deleteCacheWithName , where's the best place to put such method ? I have tried in viewWillDisappear, but with this I suppose I can have problem when the view will appear again and the cache is not present anymore, for example when using with a UINavigationController. Probably the dealloc method is the best place ?

4) What's the relationship between a cache and memory management ? I mean, when a (void)didReceiveMemoryWarning is sent do I need to delete the cache ? If yes, what about rebuilding it again ? What's the preferred way, maybe re issuing the fetch ?

5) the fetch controller has its delegate set to the UITableViewController (fetchController.delegate=self), is there any problem with that ? In a sense that in case of change more than one controllers is alerted ? And does deleteCacheWithName also remove the delegate ?

thanks

like image 551
Leonardo Avatar asked Jul 15 '11 09:07

Leonardo


1 Answers

To answer (some) your questions:

1) Having multiple caches is not a problem. In fact, from Apple's class reference:

  • If you have multiple fetched results controllers with different configurations (different sort descriptors and so on), you must give each a different cache name.

I've had issues with previous apps where I have not done this only to get an exception that I wasted time trying to resolve.

2) We are supposed to use the cache to store any repeated work the NSFetchedResultsController needs to do. Anytime the section or ordering information changes, the cache gets updated. Similarly, if the cache is not consistent with the current information in the database (you added a new row, deleted, etc) then the controller releases the cache and clears it.

3) I've personally put deleteCacheWithName in the viewDidUnload method. Not sure if this is the best place or not though.

Not sure about 4. In terms of rebuilding the cache - this should happen automatically if the current cache is out of sync with the app. I have not done anything specific for memory management and the cache on any of my apps.

5) This should not be a problem. Again, from Apple's documentation:

  • If you set a delegate for a fetched results controller, the controller registers to receive change notifications from its managed object context. Any change in the context that affects the result set or section information is processed and the results are updated accordingly. The controller notifies the delegate when result objects change location or when sections are modified (see NSFetchedResultsControllerDelegate). You typically use these methods to update the display of the table view.

FYI, here is the link to the apple dev docs for this class: http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html

HTH

like image 141
armohan Avatar answered Oct 20 '22 23:10

armohan