• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: iOS Core Data complex predicate.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: iOS Core Data complex predicate.


  • Subject: Re: iOS Core Data complex predicate.
  • From: Andreas Grosam <email@hidden>
  • Date: Thu, 17 Mar 2011 23:49:59 +0100

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 (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: iOS Core Data complex predicate.
      • From: Sandro Noël <email@hidden>
References: 
 >iOS Core Data complex predicate. (From: Sandro Noël <email@hidden>)

  • Prev by Date: What to play a series of images on, to make it look like a movie [the fastest way]?
  • Next by Date: LSUIElement making custom windows non-visible.
  • Previous by thread: iOS Core Data complex predicate.
  • Next by thread: Re: iOS Core Data complex predicate.
  • Index(es):
    • Date
    • Thread