Skip navigation.
 
mlRe: Many-to-many coredata fetch request (Probably Solved)
FROM : listposter
DATE : Wed May 31 20:25:09 2006

After a bit of searching, its apparent that if an NSManagedObject has 
a to-many or other attribute that is generated at runtime (not 
managed by the MOC)
then the fetch request will ignore it. This probably for performance, 
but I think an option to turn this off/on would be nice.

In any case, here is the workaround: after getting the fetch for the 
specific entity you want, then filter it in memory like so:

   NSPredicate *predicate = [self 
predcateThatEvaluatesNonPersistedAttributes];
   NSFetchRequst *request = [[[NSFetchRequest alloc] init] autorelease];

   [request setEntity:targetEntity];

   NSArray *resultsOfFetch = [[self managedObjectContext] 
executeFetchRequest: request error:&fetchError];
   
   if (resultsOfFetch)
   {
       return [resultsOfFetch  filteredArrayUsingPredicate: predicate];
   }


>  <email_removed> wrote:
> Andrew Cooper wrote:
>

>> I am by no means a Predicates expert, but according to
>> http://developer.apple.com/documentation/Cocoa/Conceptual/
>> Predicates/Articles/pSyntax.html#//apple_ref/doc/uid/
>> TP40001795-215891
>> of the http://developer.apple.com/documentation/Cocoa/Conceptual/
>> Predicates/ Guide, shouldn't your left and right-hand sides be 
>> switched?
>>
>> [quote]
>> IN
>>
>> Equivalent to an SQL IN operation, the left-hand side must appear 
>> in the collection specified by the right-hand side. For example, 
>> name IN { 'Ben', 'Melissa', 'Matthew' }.
>> [/quote]
>>
>> By that definition, you have your target names trying to be in 
>> people.name instead of the other way around.

>
> Hi Andew, thanks for your suggestion.
>
> The reason they were switched was because I'm getting this error:
> NSRunLoop ignoring exception 'The left hand side for an ALL or ANY 
> operator must be either an NSArray or an NSSet.' that raised during 
> posting of delayed perform with target 4b89760 and selector 
> 'invokeWithTarget
>
> This is because I am comparing items in a to-many in an array, 
> using ANY key word.
> So if the keypath evaluates to {jon,tom,bobo} and the given array 
> returns {chris,bobo} I want that to evaluate as a positive match.
>
> It seems that NSPredicate has the limitation of only being able to 
> compare to-many to a single value, not to-many IN to-many or ANY to-
> many IN to-many....
>
> Andre
>

>> Hope that helps.
>> - Andrew Cooper -
>>
>> On May 31, 2006, at 2:14 AM, <email_removed> wrote:
>>

>>> Hi all,
>>>
>>> I'm having a bit of a trouble getting  fetch to execute right.
>>> I have an entity that contains a to-many relation. In the objects 
>>> of that relation are string values I want to search for.
>>>
>>> So its like this:
>>>
>>> Entity A ->> EntityWithStrings (e.g names)
>>>
>>> So I want to do a search that takes an array of names (multiple 
>>> selection) and search for objects that contain any of those names.
>>>
>>> Someting like "ANY object.tomany.name IN myArrayOfNames"
>>>
>>>
>>>
>>> Here is how I'm constructing my predicate:
>>>
>>> NSArray *allNames = [[self selectedObjects] valueForKey:@"name"];
>>>
>>> NSExpression *targetKeyPath = [NSExpression 
>>> expressionForKeyPath:@"people.name"];
>>> NSExpression *targetNames = [NSExpression 
>>> expressionForConstantValue: allNames];
>>>         
>>> NSPredicate *weakPredicate = [NSComparisonPredicate    
>>> predicateWithLeftExpression: targetNames
>>>                                                     rightExpression: targetKeyPath
>>>                                                     modifier: NSAnyPredicateModifier
>>>                                                     type: NSInPredicateOperatorType
>>>                                                     options:0];
>>>
>>>
>>>
>>> Andre
>>> <email_removed>
>>>
>>>
>>>
>>> _______________________________________________
>>> Do not post admin requests to the list. They will be ignored.
>>> Cocoa-dev mailing list      (<email_removed>)
>>> Help/Unsubscribe/Update your Subscription:
>>> http://lists.apple.com/mailman/options/cocoa-dev/andrew.l.cooper%
>>> 40gmail.com
>>>
>>> This email sent to andrew.l.<email_removed>

>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Cocoa-dev mailing list      (<email_removed>)
>> Help/Unsubscribe/Update your Subscription:
>> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>>
>> This email sent to <email_removed>

>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list      (<email_removed>)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<email_removed>
>
> This email sent to <email_removed>

Related mailsAuthorDate
mlMany-to-many coredata fetch request listposter May 31, 09:14
mlRe: Many-to-many coredata fetch request Andrew Cooper May 31, 09:34
mlRe: Many-to-many coredata fetch request listposter May 31, 18:47
mlRe: Many-to-many coredata fetch request (Probably Solved) listposter May 31, 20:25