Core data. Programmatically setting relationships

  • Hi folks,
    Back again with more Core Data puzzles.

    I have entities like so;

    Account;
    NSString    name
    relationship    credit
    relationship    debit

    Transaction;
    NSString    name
    NSDate        date
    relationship    toAccount    (reverse of credit in Account entity)
    relationship    fromAccount    (reverse of debit in Account entity)

    I have a tableview with a custom datasource as I have to mung data
    between the Object Model and the view. Using the datasource and
    delegate methods I can display data fine and I can alter and set the
    attributes name and date.

    What I can't do is set change the relationships. In my tableView I'm
    displaying the Account name in the toAccount and fromAccount columns.
    I want to be able to type a new Account.name in there and have the
    Transaction object update the relationship to the new Account
    accordingly. Reading the core data docs I can't for the life of me
    figure out how to do this. I believe I need to take the name from the
    tableColumn, figure out which account it belongs to then somehow set
    that as the relationship by using KVC.

    Any tips?
  • Oh no, replying to my own mail!

    Fairly certain I have to do a fetch for the Account object with that
    name. So something like;

    - (void)tableView:(NSTableView *)tableView setObjectValue:(id)newValue
    forTableColumn:(NSTableColumn *)tableColumn row:(int)row
    {
    [[transactions objectAtIndex:row] setValue:fetchedAccount forKey:
    [tableColumn identifier]];
    }

    and set the transaction toAccount value to the actual fetched object
    itself.

    Can anyone confirm?

    On 01/06/2008, at 6:28 PM, Steven Hamilton wrote:

    > Hi folks,
    > Back again with more Core Data puzzles.
    >
    > I have entities like so;
    >
    > Account;
    > NSString    name
    > relationship    credit
    > relationship    debit
    >
    > Transaction;
    > NSString    name
    > NSDate        date
    > relationship    toAccount    (reverse of credit in Account entity)
    > relationship    fromAccount    (reverse of debit in Account entity)
    >
    > I have a tableview with a custom datasource as I have to mung data
    > between the Object Model and the view. Using the datasource and
    > delegate methods I can display data fine and I can alter and set the
    > attributes name and date.
    >
    > What I can't do is set change the relationships. In my tableView I'm
    > displaying the Account name in the toAccount and fromAccount
    > columns. I want to be able to type a new Account.name in there and
    > have the Transaction object update the relationship to the new
    > Account accordingly. Reading the core data docs I can't for the life
    > of me figure out how to do this. I believe I need to take the name
    > from the tableColumn, figure out which account it belongs to then
    > somehow set that as the relationship by using KVC.
    >
    > Any tips?
  • On Jun 1, 2008, at 1:28 AM, Steven Hamilton wrote:

    > What I can't do is set change the relationships. In my tableView I'm
    > displaying the Account name in the toAccount and fromAccount
    > columns. I want to be able to type a new Account.name in there and
    > have the Transaction object update the relationship to the new
    > Account accordingly. Reading the core data docs I can't for the life
    > of me figure out how to do this. I believe I need to take the name
    > from the tableColumn, figure out which account it belongs to then
    > somehow set that as the relationship by using KVC.
    >
    The documentation explains how to manipulate relationships here:
    <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdUsingMOs.html#//apple_ref/doc/uid/TP40001803-212651
    >

    Other than that, it's not clear what you're trying to achieve.
    "I want to be able to type a new Account.name in there" -- type in
    where?  What transaction are you trying to update?  How would you have
    done this if you weren't using Core Data?

    mmalc
  • Apologies for being vague, I really should learn to proof read
    properly before pressing send.

    I've sussed it out. I point the relationship attribute (wrong word I
    know) at the instance of the new object.

    On 02/06/2008, at 12:48 AM, mmalc crawford wrote:

    >
    > On Jun 1, 2008, at 1:28 AM, Steven Hamilton wrote:
    >
    >> What I can't do is set change the relationships. In my tableView
    >> I'm displaying the Account name in the toAccount and fromAccount
    >> columns. I want to be able to type a new Account.name in there and
    >> have the Transaction object update the relationship to the new
    >> Account accordingly. Reading the core data docs I can't for the
    >> life of me figure out how to do this. I believe I need to take the
    >> name from the tableColumn, figure out which account it belongs to
    >> then somehow set that as the relationship by using KVC.
    >>
    > The documentation explains how to manipulate relationships here:
    > <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdUsingMOs.html#//apple_ref/doc/uid/TP40001803-212651
    > >
    >
    > Other than that, it's not clear what you're trying to achieve.
    > "I want to be able to type a new Account.name in there" -- type in
    > where?  What transaction are you trying to update?  How would you
    > have done this if you weren't using Core Data?
    >
    > mmalc
    >
previous month june 2008 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            
Go to today