简体   繁体   中英

NSPredicate core data, fetch some objects

I have an entity, ProductCart & Cart, with a to many relationship. ProductCart has an attribute called ordered, which determine if the product has been ordered. Now I want to fetch all the objects in ProductCart which has'nt been ordered. Have tried something like this but it does not work:

NSFetchRequest *fetchRequest =  [NSFetchRequest fetchRequestWithEntityName:@"ProductCart"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"inCart == %@ && !ordered == %@", cart, [self valueForKey:@"ordered"]];

NSSortDescriptor *sortDescriptor =  [[NSSortDescriptor alloc] initWithKey:@"products.name" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];

inCart fetches the relationship, ordered is giving me problems. How can I fix this?

I expect your predicate should be more like:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"inCart == %@ && ordered == NO", cart];

because you just want to test the state of ordered rather than trying to compare it to some other value.

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