0
votes

I'm trying to build a single NSFetchRequest predicate for the following (simplified) model:

enter image description here

  • There is a simple to-many relationship between a Category and a number of Brands.

  • Each Brand then has a modelNumbersData property which is binary data, a serialisation of an array of NSString modelNumbers which are exposed in a transient property on the Brand objects.

  • There is no direct relationship between Brands and Models. The relationship is that a Model's modelNumber may be in a Brand's modelNumbers transient property.

I would like to build an NSPredicate query to fetch all of the Model objects under a particular Category.

Fetching the Models for a Brand is easy, I can do "modelNumber IN $FETCH_SOURCE.modelNumbers". How do I now extend this query to originate with the category? It seems I need a SUBQUERY?

Furthermore, I am doing an NSFetchRequest, so unless I'm mistaken I need to start with "SELF.modelNumber IN (...)", so that we select from all Models.

Thanks in advance!

1
As an update, if I first fetch ALL the Models, I can filter for what I want with: SUBQUERY(%@.brands, $brand, $brand.modelNumbers CONTAINS SELF.modelNumber).@count > 0 But how can I combine this into a single step/query? Seems it needs to be the other way around... - Jonathan Crooke

1 Answers

0
votes

Ok, I got there first. For reference, the following works:

SUBQUERY(%@.brands, $brand, $brand.modelNumbers CONTAINS $modelNumber).@count > 0

I think the problem I was mostly having was neglecting the .@count component. Still don't quite understand this, but it seems to be necessary with all SUBQUERY statements.