Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

djurgens

macrumors newbie
Original poster
May 6, 2010
3
0
Hi,

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: http://themikeswan.wordpress.com/2009/05/22/7/ 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.
 
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.

crackpip
 
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.
 
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.

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.
Code:
	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.

crackpip
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.