Reputation: 3057
I have specified a NSFetchtedResultsController as property...
@property (nonatomic, strong) NSFetchedResultsController *controller;
... and set the delegate to my class.
self.controller.delegate = self;
Then I have implemented the in the headline mentioned method and put a breakpoint in it.
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
=> BREAKPOINT <=
}
I have created the fetch request for the controller as follows:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Post"];
// ... some sort ...
self.controller = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.context
sectionNameKeyPath:nil
cacheName:@"cache"];
Now I am storing in another class in the same context some NSManagedObjects of the type "Post" but the didChangeObject
method is never called (where my breakpoint is in it).
Does anyone know what is going wrong?
Update #1: The class which fetches some posts from a server and stores it into the context is executed asynchronously using GCD.
Update #2: I have tried it in another way due it does not work to use a context in a multithreaded environment. (Thanks to svena)
Now the ServerConnection
runs in an async block (using GCD) and performs a selector on the main thread. This selector method stores the posts in the database and SHOULD update the tableview. Basically, I thought that when I store something in this context the didChangeObject
method should be called automatically? But it does not. :-/
I have added a short sequence diagram to illustrate my current status.
Does anyone know why this does not work? Please be patient. I am new to CoreData and Objective-C. :)
Upvotes: 0
Views: 1390
Reputation: 2779
Update #1: The class which fetches some posts from a server and stores it into the context is executed asynchronously using GCD.
if this is the case, how are you creating your managed object context for the GCD block? Neither contexts or managed objects are thread safe and cannot be passed around between threads. Furthermore changes made to context on one thread is not automatically propagated to contexts on other threads. Prior to iOS5 that means listening to NSManagedObjectContextDidSaveNotification and merging it there by using mergeChangesFromContextDidSaveNotification: method for your NSFetchedResultsController instance to see it.
Great diagram by the way :)
Could you please show the code at the points where you:
Upvotes: 1