Using bindings in Interface Builder, Returning only Distinct Rows

Discussion in 'Mac Programming' started by ebel3003, Mar 24, 2008.

  1. macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #1
    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
     
  2. macrumors 68000

    Fukui

    Joined:
    Jul 19, 2002
    #2
    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
     
  3. thread starter macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #3
    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.
     
  4. macrumors 68000

    Fukui

    Joined:
    Jul 19, 2002
    #4
    If your using coredata, try distinctUnionOfSets.... otherwise arrays if you have the interface builder file or the coredata model, can take a look....
     
  5. macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #5
    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.
     
  6. thread starter macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #6
    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.
     
  7. thread starter macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #7
    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?
     
  8. macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #8
    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.
     
  9. thread starter macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #9
    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.
     

    Attached Files:

  10. macrumors 68000

    Fukui

    Joined:
    Jul 19, 2002
    #10
    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......
     
  11. thread starter macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #11
    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?
     

Share This Page