Skip navigation.
 
mlRe: Can CoreData return only unique results of an attribute
FROM : Adam P Jenkins
DATE : Wed Feb 27 18:52:22 2008

Maybe the @distinctUnionOfObjects KVC operator can help.  I'm not sure 
you can use it if your ArrayController is bound directly to a 
ManagedObjectContext, but if you had another entity with a 
favoriteWebsites relationship, then you could access all of the unique 
URLs from that relationship like this:

Say the RootObject Entity has a favoriteWebsites relationship 
property, and rootObject is an instance of RootObject.  Then

[rootObject 
valueForKeyPath:@"favoriteWebsites.@distinctUnionOfObjects.url"]

would return an array of all the URLs from favoriteWebsites, with 
duplicates removed.  So you could bind the Content Array for your 
array controller and set the Model Key Path to 
favoriteWebsites.@distinctUnionOfObjects.url.

Disclaimer: I've never tried this myself, I've only read about it. 
You can read more at:

http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/ArrayOperators.html

On Feb 26, 2008, at 7:34 PM, Adam Gerson wrote:

> Thanks for the example. What I am looking for is slightly diferent.
> Lets say I have a entity called FavoriteWebsites with the attributes
> name and url. The current contents of the object are
>
> Name  |    URL
> ---------------------------------------------
> Sam    |    http://www.aol.com
> Adam  |    http://www.digg.com
> Jane    |    http://www.ibm.com
> John    |    http://www.aol.com
>
>
> I want to filter for only the unique values of url, so the list I want
> to get back for a separate table is
>
> http://www.digg.com
> http://www.ibm.com
> http://www.aol.com
>
> Perhaps NSPredicate is not my answer and I just need to maintain a
> separate array and do some manual checking for duplicates.
>
> On Tue, Feb 26, 2008 at 7:20 PM, Philip Bridson
> <<email_removed>> wrote:

>> Hi there,
>>
>> It will only return results from itself.
>>
>> Its like the classic example of employee and department. If you had 
>> a table
>> that you wanted to link to a list of employees you would bind to the
>> employee array controller and vice versa for the department one. 
>> You cannot
>> bind against the department controller for the value of, for example,
>> "Employee Name". All the predicate does is filters the list based 
>> on what
>> you want. e.g all employee with the name Joe. All you have to do is 
>> set the
>> exact predicate in the object that you are going to bind against:
>>
>> - (NSPredicate *)predicate
>> {
>> NSString *salaryLimit = @"10000";
>>
>> NSPredicate *predicate = [NSPredicate predicateWithFormat @"salary 
>> > %@",
>> salaryLimit];
>>
>> return predicate;
>> }
>>
>> Then when you bind your filter predicate to this method in your 
>> file owner
>> the array will only return objects that have a value of 10000 set 
>> in their
>> salary limit key.
>>
>> I hope I have been of assistance.
>>
>> Good luck,
>>
>> Phil.
>>
>>
>>
>>
>> On 27 Feb 2008, at 00:01, Adam Gerson wrote:
>>
>> I did look into NSPredicate and the Predicate Programming Guide. I
>> understand the concept of filtering the ArrayController. I just 
>> didn't
>> know how to write en expression asking for all unique values from the
>> ArrayController for a given key. In the Predicate examples they 
>> filter
>> a single potential result against some criteria. Can I say "only
>> return unique values from yourself"?
>>
>>
>> Adam
>>
>>
>> On Tue, Feb 26, 2008 at 6:03 PM, Philip Bridson
>> <<email_removed>> wrote:
>>
>>
>> Yeah there is.
>>
>> From the documentation:
>>
>>
>>
>> You can set a predicate for an array controller to filter the 
>> content array.
>> You can set the predicate in code (using setFilterPredicate:). You 
>> can also
>> bind the array controller's filterPredicate binding to a method 
>> that returns
>> an NSPredicate object. The object that implements the method may be 
>> the
>> File's Owner or another controller object. If you change the 
>> predicate,
>> remember that you must do so in a key-value observing compliant way 
>> (see
>> Key-Value Observing Programming Guide) so that the array controller 
>> updates
>> itself accordingly.
>>
>> You can also bind the predicate binding of an NSSearchField object 
>> to the
>> filterPredicate of an array controller. A search field's predicate 
>> binding
>> is a multivalue binding, described in Binding Types.
>>
>> Or simply, create a small method in a object, such as the file 
>> owner, that
>> returns a NSPredicate. Then bind the controller's filter predicate 
>> to the
>> file owners predicate method. This will automatically filter your
>> controllers values.
>>
>> Hope this helps.
>>
>> Phil.
>>
>> On 26 Feb 2008, at 22:00, Adam Gerson wrote:
>>
>>
>>
>> I have a core data object. I would like to populate a TableView with
>> only the unique entires for a specific property. Clearly I could
>> filter the results in code, I was wondering if there was away for 
>> core
>> data and bindings to do it.
>>
>> Adam
>> _______________________________________________
>>
>> Cocoa-dev mailing list (<email_removed>)
>>
>> 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/<email_removed>
>>
>> This email sent to <email_removed>
>>
>>
>>

> _______________________________________________
>
> Cocoa-dev mailing list (<email_removed>)
>
> 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/<email_removed>
>
> This email sent to <email_removed>