Re: NSBrowser, NSTreeController and Core Data
Re: NSBrowser, NSTreeController and Core Data
- Subject: Re: NSBrowser, NSTreeController and Core Data
- From: Quincey Morris <email@hidden>
- Date: Mon, 13 Oct 2008 15:44:06 -0700
On Oct 13, 2008, at 08:58, Mark Scardigno wrote:
My main goal here is to have core data application that implements an
NSBrowser view;
You've got a number of conceptual problems here, complicated by the
fact that Core Data is not a good place to start if you have no
experience with fundamental Cocoa technologies. But anyway ...
this being a hierarchic view of entities which I call
categories, such as...
Category -> SubCategories -> SubCategories , etc.
The sub categories selected should be based on the previous columns
selected
category, obviously.
After googling up a previous post here, I followed along and set up an
entity.
I have an entity defined, called Category which has:
+ Attribute: "name"
Type: (String)
+ Relationship: parentID
Destination: "Category"
+ Relationship: id
Destination: "Category"
To-Many Relationship
Inverse: parentID
Your relationship is from categories to subcategories (and inversely
from categories to parent categories). So calling the relationship
"id" makes no sense. IAC, to-many relationship names make more sense
if they're plural ("categories" instead of "id", and "parentCategory"
instead of "parentID").
My MainMenu.nib is as follows:
+ NSTreeController
Attributes
Mode: Entity
Entity Name: "Category"
Child Key Path: "id"
Binding
Parameters
managedObjectContext
Bind To: MyApplication_AppDelegate
Model Key Path: managedObjectContext
+ NSBrowser
Bindings
Browser Content
content
Bind To: NSTreeController
Controller Key: arrangedObjects
Model Key Path: Leave Blank
contentValues
Bind To: NSTreeController
Controller Key: arrangedObjects
Model Key Path: "name"
selectionIndexPaths
Bind To: NSTreeController
Controller Key: selectionIndexPaths
Model Key Path: Leave Blank
After I do some parsing of XML, I am programmatically creating my
category
entities and supplying core data with the values for my keys...
NSManagedObject *category = [NSEntityDescription
insertNewObjectForEntityForName:@"Category" inManagedObjectContext:
[self
managedObjectContext]];
..
[category setValue:[childNode stringValue] forKey:@"name"];
[category setValue:[childNode stringValue] forKey:@"id"];
[category setValue:[childNode stringValue] forKey:@"parentID"];
If 'childNode' is of a Cocoa class like NSXMLNode, [childNode
stringValue] is going to return the same value each time, which
doesn't look like what you want. If you're trying to get to various
XML nodes or attributes, you're going to have to do it a different
way. (Or perhaps you've invented a class that returns different
results each time.) This all suggests that you believe that the
relationships are implemented by matching of (string) names. They're
not. Relationships are object references. Core Data is an object
graph, not a database.
When you create a category object, it's going to have no subcategories
yet, so there's nothing to set for that relationship immediately. If
it has a parent, you need to find the parent object (possibly via the
name of the parent object, which is another subject), and set that
object for the parent relationship. (The inverse will get set
automatically.)
The to-many relationship is a NSSet, so if you ever need to add or
remove subcategories from an object directly, you would use NSSet
accessors. You won't use [setValue:forKey:] for that.
I realize that I'm passing a string, but according to the error, it
wants an
NSSet? How do I fix this?
I also read something about subclassing NSCell and adding the
following
methods:
- (id)objectValue
- (void)setObjectValue:(id)anObject
NSCell doesn't figure into this anywhere at all, and you should just
forget about it. You *might* subclass NSCell to customize a user
interface, but it doesn't have anything to do with your data model,
which is what's at issue here.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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