0

I'm tearing my hair out trying to get this to work.

I've had a look at all the StackOverflow related problems but I just can't get the final step to work!

I have a NSFetchedResultsController showing data from Core Data. This all works perfectly. When there is data in there it displays the data and everything is fine.

However, I have an asynchronous update that pulls down more data from an online server and saves it to Core Data.

What I am wanting to happen is that the NSFetchedResultsController picks up these updates and then displays them (i.e. animates them onto the currently displayed tableView).

The set up I have at the moment is like so................

Thanks for both the responses to this, I have had another go at trying to get this to work but I'm still not seeing the update in "real-time".

I thought I'd have a go at explaining where I am with it now. I found a few problems with my initial set up which I have now fixed but still nothing.

OK, so first the classes and what happens where...

[Class = AppDelegate]
[Has managedObjectContext (main context) plus the persistent store etc...]
[Method = mergeChanges:notifiaction this uses GCD to get back onto the mainThread and does mergeChangesWithObjectblahblah]

[Class = NetworkDataClass]
[This has its own ManagedObjectContext that gets created (in the main thread) upon init]
[Method = saveContext
1. Add AppDelegate as observer of didsavenotification of bgContext.
2. SaveContext.
3. Remove AppDelegate as observer] ( I know that the observation is working because I have a breakpoint in the AppDelegate that stops when it saves.)
[Method = getDataFromOnline (not actual name but you get the gist)
1. GCD onto bg thread.
2. Create an NSURLRequest.
3. Run the request synchronously.
4. Put the data into the class context.
5. Run self saveContext]

[Class = tableViewClass]
[Has the tableView and NSFetchedResultsController also uses NSFetchedResultsControllerDelegate]
[Method = viewWillAppear
1. run getDataFromOnline method in my network class singleton]

I have some default data in there to make sure it is being displayed and when I go into the tableViewClass (detailed above) I can see the data properly in the correct sort order with the correct section titles etc... so I can clear see that the fetchedresultscontroller is working.

I can also see (with breakpoints) that it triggers the getDataFromOnline method properly. I can see that data coming in and I can see it being added through my ManagedObject subclasses and this is all happening in the bgthread.

I can also see that when it saves the AppDelegate is running its mergeChanges function properly.

The only thing I can't see is the table being updated with the new data.

If I popToRootViewController and go back to my table then boom all the new data is there so I know that the merge has gone through and the new data has been added and merged but it just isn't appearing at merge time.

Is there something I'm missing?

Do I need to run reloadData on the table? Do I need to fire a "refetch" or something from the fetchedresultscontroller?

Really struggling with this now.

Thanks for any (more) help you can provide.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • I believe that `NSFetchedResultsControllerDelegate` methods are actually the ones that needs to be implemented for update monitoring. Also - try using view controller as observer for managed context notifications. – Eimantas Jun 17 '12 at 20:15
  • OK, sorry for one more update. I've just found out that the delegate methods ARE being called and they are on the mainThread. But still nothing is happening to the tableView. May have to look into it some more. – Fogmeister Jun 22 '12 at 18:41
  • AH! Just found it (I think) the myTableView ivar is nil when going through the delegate methods. Hmm... – Fogmeister Jun 22 '12 at 18:49
  • This means that when using the tableView it's not being connected to the xib (if it's an IBOutlet) or it's not initialized at all if it's constructed programmatically. – Eimantas Jun 22 '12 at 19:15
  • Thanks, but tried doing it as a property, ivar and just viewWithTag:99 and it is always nil?! Will try delete and re-add the tableView. Thanks – Fogmeister Jun 22 '12 at 19:22
  • Woo! OK, so all the code above works. But it helps if you load your ViewController from the correct NIB :( LOL! Thanks both for your help. – Fogmeister Jun 22 '12 at 19:44

1 Answers1

0

I'll try to give you some suggestions and redirect to some good links.

First, about you questions

Do I need to add the NSFetchedResultsController to "watch" for the merge? Or is just creating it enough to do this?

You could just simply set, as Eimantas suggested, the viewController you are using as the delegate for you NSFetchedResultsController. To do this you need to implement methods of NSFetchedResultsControllerDelegate class. When the context updates, the fetched controller will respond to changes automatically.

Where does the observer of the NSManagedObjectContextObjectsDidChangeNotification need to go? Is this OK to be in the AppDelegate (a different class than the NSFetchedResultsController is in).

Is it ok to register and merge the changes in the appDelegate. Make sure that the notification you register comes from your context and is executed in the main thread.

Are there any other observers I need to add?

No, NSFetchedResultsControllerDelegate is fine.

Is there anything I can do to see where this is going wrong? I.e. should the NSFetchedResultsControllerDelegate run any methods that I can stick breakpoints on?

First of all you could see if the core data notification contains the objects you expect for. You can simply put a log for it.

I could suggest you to read this SO question (written by me) and relative answer by Jody Hagins. Maybe it could be useful.

NSFetchedResultsController doesn't show updates from a different context

If possible, could you explain better what do you mean with

I have a NSFetchedResultsController showing data from Core Data. This all works perfectly. When there is data in there it displays the data and everything is fine.

?

Are you executing in the Core Data insertion in a different thread?

Hope that helps.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190