Search code examples
core-dataios4nsfetchedresultscontrollermodalviewcontroller

How can I suspend the working of an NSFetchedResultsController?


I have a UITableViewController fed by an NSFetchedResultsController. From it, the user can call up a modal ViewController in which he or she can enter new data. As this begins, I create a temporary object as follows:

newPtr = [[Entry alloc] initWithEntity:[NSEntityDescription
entityForName:@"Entry" inManagedObjectContext:self.nmocontext]
insertIntoManagedObjectContext:self.nmocontext];

As the user makes choices, attributes of this 'provisional' object, newPtr, are set.

The problem is that the base UITableViewController remains active while the modal ViewController is visible. It seems to be freaking out (causing crashes) in some cases when it realizes a mandatory attribute of newPtr has not been set yet.

What can I do to stop the NSFetchedResultsController from looking at my managed object context until the modal ViewController is dismissed?


Solution

  • I haven't tested this myself but a possible approach would be to implement viewWillAppear and viewWillDisappear, and set the fetchedResultsController delegate to self on will appear and nil on will disappear.

    OR

    You could create an NSObject that mirrors the attributes of your NSManagedObject in your editing window. Once the user has finished editing the attributes (and you have run the appropriate validation rules) you can pass them back to your NSManagedObject instance and let the fetchedResultsController do its job.