0

I've got an database scheme like this:

enter image description here

I initialize my fetchcontroller like this:

        NSLog(@"%s Only tracked POI query", __PRETTY_FUNCTION__);
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"POI" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];

        NSPredicate * predicate = [NSPredicate predicateWithFormat:@"rank.tracked == %@",[NSNumber numberWithBool:YES]];

        [fetchRequest setPredicate:predicate];

       NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        [fetchRequest setSortDescriptors:@[sort]];

        [fetchRequest setFetchBatchSize:20];

        result = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                     managedObjectContext:context sectionNameKeyPath:nil
                                                                cacheName:nil];

I've got a Rank entity like you see on the screenshot. When I set the tracked (boolean) to YES this fetchcontroller should show new POI's. Because I retrieve all the POI's which Rank.tracked == YES.

It works but it's always one step behind. I click on a tracked Rank nothing happens, when I click on another Rank to track then the tableview updates with the changes off the previous click.

My tableview is extending https://gist.github.com/CloudNiner/8681866 this stanford class.

Can someone explain me why it's not updating straight away? I found this on SO Changing a managed object property doesn't trigger NSFetchedResultsController to update the table view

But I don't see how it could help me.

user1007522
  • 7,858
  • 17
  • 69
  • 113
  • The linked answer tells you that the FRC is monitoring the POI for changes, but you aren't changing them so it doesn't get any notifications. Instead, you are changing the Rank object, but it isn't being monitored. The answer gives you a 'workaround' which changes the relationship on the object to the same thing, so no change is actually made, but a notification is fired. – Wain Sep 12 '14 at 08:24
  • An alternative would be to manually trigger a refresh when the Rank changes, but it isn't great. – Wain Sep 12 '14 at 08:25
  • So to do it on the nice way. I need to create a tracked attribute in my POI too? – user1007522 Sep 12 '14 at 08:55
  • You don't necessarily need a new attribute, you could just re-set the relationship to the same contents. The important part is that a setter is called on the POI objects associated with the changed Rank object, as that is the trigger for the change notification to be sent to the FRC. – Wain Sep 12 '14 at 09:00
  • Can you show me in code on my example please? – user1007522 Sep 12 '14 at 09:02
  • Try writing a `for` loop, using `rank.poi`. On each iteration, set `poi.rank = rank`. – Wain Sep 12 '14 at 09:09

0 Answers0