Can someone explain contents in SUBQUERY?

Everything I've found about SUBQUERY has told me about half of what I need to know. Sorry if the following sounds stupid


I know the contents portion of SUBQUERY is the name of a propery, either NSARRAY or NSSet.

Should the property be allocated & initialized before the query is executed? Should it be somehow populated? Aside from being named in the SUBQUERY argument, how should it be used ?


Thanks in advance.

The best explanation I've found for how to use a subquery and what it does is "What the heck is subquery?"

http://funwithobjc.tumblr.com/post/2726166818/what-the-heck-is-subquery

While I wait for the link in the other post to be moderated... (If it doesn't show up, do a Google Search for "What the heck is SUBQUERY?" and read the Fun with Objective-C blog entry.)


"Should the property be allocated and initialized before the query is executed? How should it be populated?" I'm concerned that you're misunderstanding how the expression works.


A simple subquery looks like this:

SUBQUERY( collection, $x, $x.property = 'foo' and $x.number = 42)

That's the subquery expression for: the values from collection that each (individually) match "property = 'foo' and number = 42".

That syntax exists because

ANY collection.property = 'foo' AND ANY collection.number = 42

can be satisfied by two different members of collection. In contrast

SUBQUERY( collection, $x, $x.property = 'foo' and $x.number = 42).@count > 0

isn't true unless the same member of collection has 'foo' and 42 for its values.


Subquery takes three arguments:

1. An expression which evaluates to a collection of values

2. A variable name that you're going to use to refer to the indivual values of the expression in #1

3. A predicate containing the variable name from #2 which is going to be evaluated for each value in #1

and results in the collection of the values that passed the predicate.


If you have a to-many relationship on an object, that's a convenient value to use as an expression for argument #1. But you could just as well use the value of another subquery expression, or any of the other expressions that evaluate to collections.

You can also check out this article: https://medium.com/@Czajnikowski/subquery-is-not-that-scary-3f95cb9e2d98

Can someone explain contents in SUBQUERY?
 
 
Q