简体   繁体   中英

Core Data - NSPredicate to filter to-many relationship

I have 2 entities, Task and List. Each task has a to-one relationship to a List object called "list", and there is an inverse relationship with List, which has a to-many relationship with Task called "tasks".

I'm trying to use a fetch request with an NSPredicate to get all the Task objects that belong to a specified List:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"list=%@", theList];
[fetchRequest setPredicate:predicate];

(where "theParent" is a reference to a List object). However this returns no fetched objects. If I take out the predicate, then the objects are returned (so I do know they exist, and by NSLogging theList I know it has Task objects associated with it).

Thanks

Could it be a simple typo in your predicate?

You have "list=%@" when I assume what you really want is "list==%@"

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"list == %@", theList];
[fetchRequest setPredicate:predicate];

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