Core data application - show related entries in table

Discussion in 'Mac Programming' started by djurgens, May 6, 2010.

  1. djurgens macrumors newbie

    May 6, 2010

    I'm new with programming for Mac (or any non-web based platform for that matter) so please be gentle as this might be a very dumb question.

    I have a (document based) core-data application that has two entities that are related through a one-to-may relationship. I want to show data in a single window where the user selects an entry in a table field, and data is displayed related to this entry.

    I have gotten so far that the data from the selected entity is shown, but now I also want it to show the related data from the other entity. How do I get this to work? I have read through this tutorial: but the method used here doesn't seem to work for me.

    Please let me know if you need additional information.

    Thanks in advance for your help.
  2. crackpip macrumors regular

    Jul 23, 2002
    There are three main points.

    1) Create two NSArrayControllers one for each entity. For example, I have entity bookshelf that has a to-many relationship to an entity, book. The entity book will have a to-one relationship with bookshelf. I create two NSArrayControllers, one for books and one for bookshelves.

    2) Bind the primary array controller to the NSColumn of the NSTableView, and bind the other array controller to the column of the secondary NSTableView. So the primary table gives me a list of the name attribute for each bookshelf, and when I click on a bookshelf, the secondary table shows a list of the name attributes of the books.

    3) Bind the Content Set of the secondary NSArrayController to the primary array controller using a key "selection", and a model key path of the to-many relationship. Now the books array controller will only contain the NSSet of books that have the selected bookshelf as the to-one relationship bookshelf.

  3. djurgens thread starter macrumors newbie

    May 6, 2010
    Great! It seems to work, it was really not that complicated. Thanks!

    Just a quick question: a new "book" created when an "bookshelf" is selected, is always related to this bookshelf? It does seem so, which seem logical, but I want to make sure I know what I'm doing.
  4. crackpip macrumors regular

    Jul 23, 2002
    Yes, the CoreData bindings must take care of it automatically presumably by using the inverse relationship between the two entities. So for example if you wanted to assign a book to a different bookshelf you could take the book and the new bookshelf.
    	NSManagedObject *newBookshelf = ...
    	NSManagedObject *book = ...
    	[book setValue:newBookshelf forKey:@"bookshelf"];
    The book would now be associated with the new bookshelf and your NSTableViews would automatically update.

  5. djurgens thread starter macrumors newbie

    May 6, 2010
    Great, I understand (I think).

    Thanks for your help.

Share This Page