开发者

Predicate and distinct objects

开发者 https://www.devze.com 2023-01-09 23:46 出处:网络
I have entity Unit and Tag, each with to-man开发者_如何转开发y relation to other. I am using NSFetchedResultsController to manage the data. What I need is to return distinct Unit object into NSFetch

I have entity Unit and Tag, each with to-man开发者_如何转开发y relation to other.

I am using NSFetchedResultsController to manage the data. What I need is to return distinct Unit object into NSFetchedResultsController for condition Tag.show == YES. I'm not sure how to feed all this to NSFetchedResultsController. Set entity to Unit or Tag, how to build predicate for it.

Example:

I have 6 Tag objects tag1...tag6 and 3 Unit object unit1, unit2, unit3. tag1, tag2 are pointing to unit1, tag3, tag4 to unit2. tag1...tag4 met show == YES condition. So I want finally to get uni1 and unit2 into NSFetchedResultsController.


Do a fetch on the Unit entity with a predicate of "ANY Tag.show==YES".

That will return any Unit instances that has one or more related Tag instances with show==YES. Fetches return distinct objects so you don't have to do anything else.


I tried this on one of my problems.

If have a NSMutableArray filled with RSSItem objects, which - amongst others - contain a property link, which is a NSString.

Now I want to find all objects in the array, which's link is equal to another NSString [item link].

NSPredicate *predicate = 
      [NSPredicate predicateWithFormat:@"ANY link == '%@'", [item link]];
NSArray *filteredArray = 
      [[delegate itemArray] filteredArrayUsingPredicate:predicate];
if ([filteredArray count] > 0) {
      // IF such an object exists, do something ...
}

Isn't that the same thing, you were trying? Or am I misunderstanding something about predicates here?

Greetings, Dodo

0

精彩评论

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

关注公众号