简体   繁体   中英

NSFetchedResultsController delegate callback for relationship

Suppose you have two entities, one is People , the other is Location . Location has an attribute name , People has a to-one relationship to Location named location .

Then if you have an NSFetchedResultsController like below, you can't get the delegate callback when you change the Location s name .

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"People"
                                          inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"location.name" 
                                                               ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];

[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController *aFetchedResultsController = 
   [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                       managedObjectContext:self.managedObjectContext
                                         sectionNameKeyPath:@"location.name" 
                                                  cacheName:@"Master"];

aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

I know I can fetch the Location entity and then filter it by name to solve this problem. But I'm curious about this, does the NSFetchedResultsController delegate just notify you when something changed in the entity's relationship,not the relationship's attribute.

Can anyone give me some posts about this feature. I search the web and Apple's documentation but can't find a reasonable description.

does the NSFetchedResultsController delegate just notify you when something changed in the entity's relationship,not the relationship's attribute.

Exactly. Because a change in the relationship is a change in the object that the FRC is observing. But a change in the properties of one of the objects at the other end of the relationship will not be notified to the delegate because those objects are not being observed by the FRC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM