• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Many-To-Many Relationship Interface in Core Data
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Many-To-Many Relationship Interface in Core Data


  • Subject: Re: Many-To-Many Relationship Interface in Core Data
  • From: "Frank D. Engel, Jr." <email@hidden>
  • Date: Wed, 25 Apr 2007 22:02:48 -0400

I will try that when I get a chance (I need to stop for tonight), thank you!

I was sort of hoping there might be some tricky way to do this using Bindings, but I'm happy to give this a try instead.


To clarify the problem, in case anyone had a suggestion for an alternate interface or anything, let me give a quick "parallel" problem, the solution to which would be the solution to mine:


Suppose a company makes and sells guitars. A given guitar might have a specific shape, and would be made with a particular type of wood. Not all types of wood are available for all shapes.

This might be represented by two entities, Shape and Wood, with a many-to-many relationship between them to determine which shapes of guitar can be made with which types of wood.

So a Shape might be:


Shape name attribute base_price attribute --- availableWithWoods relationship (one-to-many) to Wood



And a Wood might be:

Wood
	name					attribute
	price_modifier			attribute
	---
	canBeUsedForShapes	relationship (one-to-many) to Shape



I have the interface (to my parallel problem) for defining the individual Shapes, and for defining the types of Wood.

What I am looking for is something that, for a selected Shape, lets me check off (or otherwise indicate) which types of Wood can be used for that particular Shape.


It looks like the below will work, so I plan on trying that next time I get a chance to work on this, unless someone can come up with a "lazier" alternative that still provides a clean interface...



Thank you!


On Apr 25, 2007, at 9:47 PM, mmalc crawford wrote:


On Apr 25, 2007, at 5:49 PM, Frank D. Engel, Jr. wrote:
What I want to do (ideally) is add a new NSTable on the same tab as the one for A, which will list all of the "rows" from B, with checkboxes next to them, such that rows in B related to the selected row from the table for A will be checked, and they will be unchecked otherwise (with the checkboxes clickable to change the relationships).
[...]
Even better, could someone give me at least a sketchy outline with the steps to set this up (or some similarly functional interface)?


The data source for the relationship table for view must, obviously, know what is the currently-selected item (or are the currently-selected items if you want to support that) in table A. Given that, you can implement a data source for the relationship table view:

- (int)numberOfRowsInTableView:(NSTableView *)bTableView
{
    return <number of objects of type B>;
}



- (id)tableView:(NSTableView *)bTableView
objectValueForTableColumn:(NSTableColumn *)bTableColumn
row:(int)rowIndex
{
if ([[bTableColumn identifier] isEqual:<anything except the checkbox column identifier>])
{
return <the value appropriate for the row and column>;
}


if ([[bTableColumn identifier] isEqual:<the checkbox column identifier>])
{
NSMangagedObject *moB = <instance of B at rowIndex>;
NSManagedObject *soA = <selected instance of A>
NSSet *relationshipSet = [soA valueForKey:@"relationshipName"];
BOOL b = [relationshipSet containsObject:moB];
return [NSNumber numberWithBool:b];
}
}



If you want to support a multiple selection in table view A, the implementation will obviously be more sophisticated. You'll have to iterate through the selected objects and determine whether moB is a member of the relationship for each of them. If it is for all, return NSOnState; if it is not for all, return NSOffState; if it is mixed, return NSMixedState (wrapped in an NSNumber in each case).


The implementation of tableView:setObjectValue:forTableColumn:row: follows the same pattern, except if the passed-in object's value is 1 then add moB to the relationship if it is absent, and if the passed-in object's value is 0, then remove moB from the relationship if it is present.

mmalc





_________________________________________________________________ Need personalized email and website? Look no further. It's easy with Doteasy $0 Web Hosting! Learn more at www.doteasy.com _______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Many-To-Many Relationship Interface in Core Data (From: "Frank D. Engel, Jr." <email@hidden>)
 >Re: Many-To-Many Relationship Interface in Core Data (From: mmalc crawford <email@hidden>)

  • Prev by Date: Re: Implementing Table Selection Support for Custom Controller?
  • Next by Date: Re: Appending string to NSTextView crashing app
  • Previous by thread: Re: Many-To-Many Relationship Interface in Core Data
  • Next by thread: Re: Many-To-Many Relationship Interface in Core Data
  • Index(es):
    • Date
    • Thread