简体   繁体   中英

How do I fetch the child entities of a parent object in Core Data?

I'm trying to use NSFetchedResultsController to get all the child entities of a particular parent object. This seems like it should be really simple, but I'm getting "unable to parse this format string" on the NSPredicate. Any ideas?

Note: I know I could just reference parent.children instead of using NSFetchedResultsController, but this is for a live tableView and I want the object monitoring capabilities that NSFetchedResultsController offers.

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

NSString *predicateString = [NSString stringWithFormat:@"parent == %@", self.parent];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
[fetchRequest setPredicate:predicate];

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];

predicateWithFormat requires different arguments than given.

Try:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parent == %@", self.parent];

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