views:

40

answers:

2

Not even sure if this is feasible, but here's the use case:

I have a RootView tableview that uses NSFetchedResultsController to manage the list. I tap an add button, which presents the AdditionViewController. The AdditionViewController uses a separatemanagedObjectContext to create the new object. On Save, the object is passed back to the RootView, where the new object (in the new managedObjectContext) is merged into the main managedObjectContext. The AdditionViewController is then dismissed, revealing the RootView.

What I would like to do, is to push my DetailViewController with the new object loaded after the merge, so that when the AdditionViewController is dismissed, the full detail view is revealed.

How can I get the object that has just been added to the fetchedResultsController in order to pass it to the DetailViewController?

--UPDATE--

Still nothing on this. Let me try to explain what I need to do (hopefully) a bit better. If this is still confusing, ask questions. I'm having a hard time thinking of how to describe the problem.

I am fully aware how to push the Detail view underneath the modal addition view upon saving the new object. The problem is that the object I am saving is in its own fetchedReaultsController. I am merging this frc into the main fetchedResultsController, so if I try to sent the object to the detailview, I get a crash, because the object has been invalidated (due to the merge) by the time the modal addition view is dismissed, and the detailview calls viewWillAppear. That is what I am trying to get around. How can I figure out what object was just added to the main fetchedResultsController in order to send it to the detailViewController?

--UPDATE--

Adding a bounty for anyone who can tell me how to retrieve the most recently added object from a fetched results controller. Or how to retrieve a specific object from a fetched results controller without knowing it's indexPath.

A: 

Here's how I did it in an almost identical use case:

  1. While the AdditionViewController is displayed, the user has the option of saving the item they created or cancelling out of the new item dialog. I communicated the user's choice back to the RootViewController.
  2. If the user cancelled, remove the object you created from your context.
  3. If the user chose to save, save the context and display the DetailViewController.
kubi
Yeah, I can get that to work. I already have a save bool implemented. What I can't figure out is how to get the object that was just saved in order to pass it to the `DetailViewController`.
Gordon Fontenot
how are you creating the managed object before you save it? Just hold a reference to it and pass it back to the delegate.
kubi
I can do that too, but it's in a different managed context. So by the time the detailview is shown, the object has become invalidated due to the merge.
Gordon Fontenot
You could pass back the object ID instead of the whole object and then refetch it in the new context.
frenetisch applaudierend
A: 

For the record, the answer was to grab the object ID of the object in the addingManagedObjectContext AFTER saving the context (since the ID changes after saving), and passing that ID to the main managedObjectContext after the merge. The full code required for this is below (if anyone has an easier way, let me know)

detailViewController.object = (customObject *)[[fetchedResultsController managedObjectContext] objectWithID:[[[[addingManagedObjectContext registeredObjects] allObjects] objectAtIndex:0] objectID]];

Thanks to frenetisch applaudierend for pointing me in the right direction.

Gordon Fontenot