On Mar 17, 2011, at 8:15 PM, Sandro Noël wrote:

> Greetings!
> 
> I am facing a problem with a complicated predicate I'm building.
> 
...

> then this predicate is fed to a fetch Request which in turn is fed to a 
> fetched Result Controller.
> the fetch is configured to    [fetchRequest setReturnsDistinctResults:YES];
> 
> The problem as stated in the introduction this complete predicate returns 
> multiple instances of the same record and it should not.

About setReturnsDistinctResults:
"If YES, the request returns only distinct values for the *fields specified by 
propertiesToFetch*."

That is, setReturnDistinctResults: makes only sense IFF you also specified the 
set of properties you want to fetch (via -setPropertiesToFetch). And this 
requires that you specify NSDictionaryResultType for the resultType property of 
the fetch request. As a result, with setReturnDistinctResults:YES you get a set 
of dictionaries whose values are distinct.

In other words, you cannot use -setReturnsDistinctResults:YES to make your 
result set distinct if this array contains *managed objects*.


You might use a NSSet which you initialize from your original array of managed 
objects in order to get a unique set.

Alternatively, you might fetch just objectID properties using a 
NSDictionaryResultType. This, however, is a bit elaborated:

When you return (unique) dictionaries as objects in your result array, and if 
you want these dictionaries having a key "objectID" which value corresponds the 
actual managed object of this dictionary instance, you need to create an 
appropriate NSExpressionDescription: and include this property description in 
the array which you pass to -setPropertiesToFetch:

NSExpressionDescription* objectIdDescription = [[NSExpressionDescription alloc] 
init];
[objectIdDescription setExpression:[NSExpression expressionForEvaluatedObject]];
[objectIdDescription setExpressionResultType:NSObjectIDAttributeType];

Note: NSExpressionDescription is subclassed from NSPropertyDescription.

Then you use objectIdDescription as one of the properties 
(NSPropertyDescription) you want to fetch:
[myFetchRequest setPropertiesToFetch:[NSArray arrayWithObject: 
objectIdDescription]];

Don't forget to set the result type (after you set the entity):
[myFetchRequest setResultType: NSDictionaryResultType];
[myFetchRequest setReturnsDistinctResults:YES];
...


The next step would be to extract the objectIDs from the array of dictionaries 
and store them in an array for convenience.


Regards
Andreas


> if any of you see a flaw in the logic or anything, please comment, I'm 
> currently so deep into it, I cant see it anymore. :)
> 
> I hope I have not missed nay details.
> Thank you!!!
> Sandro.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to