Core data predicate and expression madness?

When you use setPropertiesToFetch: you are telling the fetch to ignore all other properties. This means your predicate of a == % which is keyed on the a attribute, is going to be ignored.

When you use setPropertiesToFetch: you are telling the fetch to ignore all other properties. This means your predicate of @"a == %@" which is keyed on the a attribute, is going to be ignored. What you probably want to do in this case is to convert the @"a == %@" predicate into an NSSubqueryExpressionType equality expression and then make that the first expression in the array passed to setPropertiesToFetch:.

That will direct the fetch to first find all B objects that match @"a == %@" and then to run the v expression against that set of objects. However, when you find yourself having to create convoluted predicates, that is usually an indication that your data model is poorly designed. Quite often, you end up with complex predicates because you are trying to create logical relationships within a predicate instead of modeling them in the data model.

Further, as a rule, when you have a particular object in a hand e.g. SomeAand/or someC then you don't do a fetch at all but instead you walk the relationships. Why fetch all B objects related to the someA object when you can just use someA. BObjects?

I think your data model really looks like this: A>BSo, the first thing to do is to get the intersection of the sets: NSMutableSet *secSet=NSMutableSet setWithSet:someA. Bs intersectSet:someC. Bs; … now all you have to do is find the B object with the most recent date: BClass *someB=secSet valueForKeyPath:@"@max. DateAttribute"; … and you're done.

Iterate the process to find the intersections between multiple A and C objects.

To your first point (about the predicate being ignored) - I don't think it is, because the code above works correctly. And I think I am probably going to have to do this calculation 'in code' rather than 'in the database'. My instinct is to try to 'write sql' (via the correct core data op) to do that calculation, rather than in code, since doing it in code will bring up every object that is the target of relationships, and there could be many.

Maybe that's just the wrong way to think. And finally, regarding "@max. Attribute" - thanks, I didn't know that was possible! – Colin Aug 24 at 18:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions