PDA

View Full Version : Using bindings in Interface Builder, Returning only Distinct Rows




ebel3003
Mar 24, 2008, 10:29 PM
Hey MR programmer community,

I'm pretty new to Cocoa (and extremely new to the notion of coding many things without actually writing code) and just started buckling down and learning it this week. I'm extremely excited about Core Data and cocoa bindings. I've gotten myself stuck on one thing, though.

Say I have a Core Data model built like this:
The top of the tree is called Person.
Person has two String attributes, one called name and the other called city

Now suppose I create another NSTableView and assign the table column to People.city. I want to filter out any duplicates into that NSTableColumn so that it will only display unique/distinct rows. Now I've read about key operators like "@distinctUnionOfArrays", but I'm not exactly sure how to go about implementing them. It can be done via the Bindings inspector, right? Could someone help me out here? :)

Thanks,
Ebel3003



Fukui
Mar 24, 2008, 10:44 PM
Hey MR programmer community,

I'm pretty new to Cocoa (and extremely new to the notion of coding many things without actually writing code) and just started buckling down and learning it this week. I'm extremely excited about Core Data and cocoa bindings. I've gotten myself stuck on one thing, though.

Say I have a Core Data model built like this:
The top of the tree is called Person.
Person has two String attributes, one called name and the other called city

Now suppose I create another NSTableView and assign the table column to People.city. I want to filter out any duplicates into that NSTableColumn so that it will only display unique/distinct rows. Now I've read about key operators like "@distinctUnionOfArrays", but I'm not exactly sure how to go about implementing them. It can be done via the Bindings inspector, right? Could someone help me out here? :)

Thanks,
Ebel3003
Yes... almost, bind the table view to the people array controller, and have the path set to @distinctUnionOfObjects.city.... if city returned an array, then you would use distinctUnionOfArrays...

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

ebel3003
Mar 24, 2008, 11:14 PM
Thanks for the reply and documentation link. That's exactly what I've been doing, yet it doesn't return any entries no matter what I do. (I am using @distinctUnionOfObjects.city in the Model Key Path.) Straight "city" returns all of the entries, yet adding @distinctUnionOfObjects. as a prefix does not return any information. There has to be something I am missing, but I just can't figure it out.

Fukui
Mar 25, 2008, 12:04 AM
Thanks for the reply and documentation link. That's exactly what I've been doing, yet it doesn't return any entries no matter what I do. (I am using @distinctUnionOfObjects.city in the Model Key Path.) Straight "city" returns all of the entries, yet adding @distinctUnionOfObjects. as a prefix does not return any information. There has to be something I am missing, but I just can't figure it out.
If your using coredata, try distinctUnionOfSets.... otherwise arrays if you have the interface builder file or the coredata model, can take a look....

lucasgladding
Mar 25, 2008, 12:12 AM
You may need to bind to People.arrangedObjects.@distinctUnionOfArrays.name or .city. The operator must be sent to an array (not the array controller). I have only worked with keyPath operators once or twice, but hopefully this helps.

Also, you need to make sure that the managedObjectContext is bound for the array controller and that the entity is set. I assume this has been done already, but it is worth mentioning. I always find it easiest to bind to arrangedObjects.name or .city before adding the operators.

ebel3003
Mar 25, 2008, 12:17 AM
Fukui, Here's a project that I've been having that issue with (I was trying to apply Core Data to this app originally, but later began to experiment with People.city). I've also noticed that when both controls are in the same windows and the second NSTableView is attempting to access the same array controller, it locks up the interface. Not sure if that is the exact cause, but being new to cocoa, that is what is apparent.

http://fsandoval.net/MP3Library.zip

I'm trying to return the distinct rows to the NSTableView on the right.

ebel3003
Mar 25, 2008, 11:37 AM
You may need to bind to People.arrangedObjects.@distinctUnionOfArrays.name or .city. The operator must be sent to an array (not the array controller). I have only worked with keyPath operators once or twice, but hopefully this helps.

Also, you need to make sure that the managedObjectContext is bound for the array controller and that the entity is set. I assume this has been done already, but it is worth mentioning. I always find it easiest to bind to arrangedObjects.name or .city before adding the operators.

So, from what I'm hearing, I should not be requesting the distinct values from the NSArrayController with the NSTableView, but I should be requesting distinct values from the Model with the NSArrayController instead?

lucasgladding
Mar 25, 2008, 01:58 PM
So, from what I'm hearing, I should not be requesting the distinct values from the NSArrayController with the NSTableView, but I should be requesting distinct values from the Model with the NSArrayController instead?

Sorry, my description was a little unclear. I was assuming you named the array controller People. You should be binding the NSTableColumn to the array controller for the Person entity and use the keyPath arrangedObjects.@distinctUnionOfArrays.name. I just meant that you need to get the array from the array controller through arrangedObjects before you can apply the operator.

ebel3003
Mar 26, 2008, 06:08 PM
Sorry, my description was a little unclear. I was assuming you named the array controller People. You should be binding the NSTableColumn to the array controller for the Person entity and use the keyPath arrangedObjects.@distinctUnionOfArrays.name. I just meant that you need to get the array from the array controller through arrangedObjects before you can apply the operator.

Thanks for the explanation, I think I get what you're saying. It's a bit confusing, since I'm new to bindings, but I'll keep playing around.

I attached a screenshot of NSTableColumn's binding window, maybe someone can point out what I'm not inputting correctly. I get the idea.. I just can't seem to make it work. Whether I use "Arrays", "Objects", or "Sets", behind distinctUnionOf, none work.

Fukui
Mar 26, 2008, 09:39 PM
Please take a look at the changed project you sent me.
homepage.mac.com/son_gohan/NukeMP3CoreData.zip

I'm kinda busy so I'll write in detail later, but take a look at the bindings for the artists and artists array controller, and also take a look at the data model file. You'll have to erase your old data file in the library/applications support folder or you'll get an error.

Basically, for what your trying to do, its better to separate the values into different entities and use relationships......

ebel3003
Mar 26, 2008, 10:28 PM
Please take a look at the changed project you sent me.
homepage.mac.com/son_gohan/NukeMP3CoreData.zip

I'm kinda busy so I'll write in detail later, but take a look at the bindings for the artists and artists array controller, and also take a look at the data model file. You'll have to erase your old data file in the library/applications support folder or you'll get an error.

Basically, for what your trying to do, its better to separate the values into different entities and use relationships......

Thanks for the response and changes. Actually seeing it done is a lot easier for me to learn, and this is going to help me a lot. I noticed that there is a separate controller for Distinct artists and then All artists, is it common practice to create many array controllers in core data applications? How many is too many?