CoreData custom view on NSTableView

  • Hi everyone,

    I am working on an application that uses CoreData to store phone
    numbers captured from a socket of the Asterisk Manager Interface. The
    number can then be displayed in a window so the user can see lists of
    outgoing, incoming, missed calls. All this works.

    I would like to look up the Apple address book with each of the phone
    numbers and see if they an entry and display say their name and a
    picture if its available. Obviously the neat thing would be tot do
    this when the table is displayed rather than when I save the data so
    any changes to the address book data is reflected when the table is
    displayed.

    So my thoughts are, is there a way I can write say an instance methods
    to the classes/entities in the CoreData model and then bind each
    column to call those methods to return their looked up data?

    Other suggestions are welcome. I love Core Data and would like to use
    it and I am quite certain I am not the first one to have this
    requirement.

    Please help, appreciate your time.

    PS I am using Leopard and XCode 3

    --
    "I never look back darling, it distracts from the now", Edna Mode (The
    Incredibles)
  • On Dec 15, 2007, at 4:53 PM, Devraj Mukherjee wrote:

    > Obviously the neat thing would be tot do
    > this when the table is displayed rather than when I save the data so
    > any changes to the address book data is reflected when the table is
    > displayed.
    >
    > So my thoughts are, is there a way I can write say an instance methods
    > to the classes/entities in the CoreData model and then bind each
    > column to call those methods to return their looked up data?

    Absolutely.  Just create an NSManagedObject subclass for the entity
    your table is showing, and add the appropriate methods to it.

    There's nothing special about Core Data here.  You can invoke any zero-
    argument method that returns an object or scalar value using Key-Value
    Coding, which is all the NSArrayController that you're using to manage
    the table is doing.

      -- Chris
  • Devraj:

      I only skimmed, so forgive me for any mistakes ...

    > I would like to look up the Apple address book with each of the phone
    > numbers and see if they an entry and display say their name and a
    > picture if its available.

      This isn't really a Core Data problem, exactly. Bindings operates
    independently of Core Data, and all you're really trying to do is get
    a bound table column to display some data.

      In order to do that, simply review the Key Value Coding related
    documentation and design an appropriate accessor. Something like the
    following:

    - (NSString *)addressBookName
    {
      // ask self for phone number (via keypath so NSManagedObject
      // faults are automatically fired, etc.), search the address
      // book records for a match, see if a phone number exists for
      // the match, return phone number as string ... otherwise,
      // just return the phone number
    }

      Since your table column will be bound to the
    "addressBookName" (which is KVC compliant), a managed object in a Core
    Data context will "do the right thing" and look up a related address
    book entry. Failing that, it will return the original data upon which
    the name lookup is based (the telephone number the managed object
    already "knew").

      The picture is handled similarly. I'd suggest, however, that you
    don't look it up in the AddressBook database each time it's displayed
    (read the documentation - many cells are potentially displayed in
    short order, so such things should be quick). I'd instead cache the
    value the first time it's requested (within the managed object
    instance in this case) and return the cache on each subsequent call.
    Of course if the AB database is changed between calls, the cache would
    be invalid, but 1) how often is that likely to happen and, 2) you
    could always implement a "time-to-live" on your cache.

      That's how I'd do it - at least tonight. As so often I do on
    weekends, I'm replying after enjoying my favorite Beaujolais. :-) Take
    my advice with a grain of salt (or a bottle of Beaujolais).

    --
    I.S.
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today