开发者

CoreData - NSPredicate results if relationship has data

开发者 https://www.devze.com 2023-03-31 09:09 出处:网络
I have a many-to-many relationship between tables, and I populate a tableView with Activities. For that i user a simple NSPredicate like this:

I have a many-to-many relationship between tables, and I populate a tableView with Activities. For that i user a simple NSPredicate like this:

request.predicate = [NSPredicate predicateWithFormat:@"deleted == %@", [NSNumber numberWithBool:NO]];

CoreData - NSPredicate results if relationship has data

How can I do to show only the Activities that has Members attached to it?

I think that in the NSPredicate I have to do some count so that only the Activities with count > 0 are returned. Is that so? H开发者_如何学Pythonow?

(i'm newbie in coredata...)

Thanks,

RL


You need to add a subquery to your predicate acting on the CompanyActivity entity as follows:

[[NSPredicate predicateWithFormat:@"deleted == %@" && (0 >= SUBQUERY(Members, $sub, $sub.deleted == %@).@count)", [NSNumber numberWithBool:NO] [NSNumber numberWithBool:NO]];

The first part of the predicate returns objects which have not been deleted, the second one related to the subquery will take care of retrieving all those CompanyActivity objects whose Members have not been deleted.

0

精彩评论

暂无评论...
验证码 换一张
取 消