Search code examples
objective-ccore-datansfetchedresultscontrollernsfetchrequest

NSFetchedResultsController delegate methods not firing for these changes


I thought this would work, but I'm not sure why I am doing wrong. I have a fetched results controller using the predicates in the code below:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription entityForName:@"SearchResult" inManagedObjectContext:[[DataInterfaceObject sharedAlloc] managedObjectContext]];    
[fetchRequest setEntity:entity];    
[fetchRequest setFetchBatchSize:10];

// Results Table predicate
NSPredicate *predicate_Results = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: [ResultsTableVC resultsPredicate], [NSPredicate predicateWithFormat:@"searchUsed.isCollapsedView == NO"], nil]];

// Favorites Table predicate
NSPredicate *predicate_Favorites = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: [FavoritesTableVC favoritesPredicate], [NSPredicate predicateWithFormat:@"favoritesInfoLink.isCollapsedView == NO"], nil]];

// Full predicate
NSPredicate *predicate = 
[NSCompoundPredicate orPredicateWithSubpredicates: [NSArray arrayWithObjects: 
                                                    predicate_Results, predicate_Favorites, nil]];

[NSFetchedResultsController deleteCacheWithName:@"resultsCache"];
[fetchRequest setPredicate:predicate];

The intent is to fetch all SearchResult entities that match the predicate. A SearchResult entity has a searchUsed relationship like this: SearchResult <<--> SearchTerms. SearchTerms has a property isCollapsedView.

The user can tap a view and cause the corresponding object view to collapse, and at that time the value of searchUsed.isCollapsedView is updated and saved to the moc. I have verified that this is happening.

I expected this change to be recognized by the fetched results controller and then the delegate methods would fire, but they don't.

The delegate methods do fire when search entities are inserted, deleted, or updated.

Do I have this set up wrong, or are my expectations wrong? What can I do about it?


Solution

  • I found that it's enough to just touch a property of the entities of the type referred to in the entity assignment. Then the entity is identified by the moc as an change/update. Apparently, at that point the frc will apply the predicate (including relationships) to those changed entities to determine if there is an insert, delete, or update. (By touching, I mean just assigning a value that is identical to the existing value and then saving it to the mod.) Doing this avoids adding properties that are already captured in the relationship entities.