Re: Embedding a database
Re: Embedding a database
- Subject: Re: Embedding a database
- From: Chris Hanson <email@hidden>
- Date: Sat, 10 Jun 2006 21:09:27 -0700
On Jun 10, 2006, at 6:47 PM, Thom McGrath wrote:
CoreData does not appear to be a valid option here (possibly
because I never liked CoreData, found it to be too restrictive)
because I need an iTunes-like library where a single object can be
found in multiple collections. I have Collections, Categories, and
Objects. User can create Collections and move Objects into them at
will. Objects have a Category, and the user can browse by Category.
So thus, a Collection would have Categories associated as well. The
interface becomes more complicated than CoreData and bindings
appear to be able to handle, so I'm doing this "by hand" rather
than relying on "magic".
As you've described your data model above, Core Data can handle it.
Core Data fully supports many-to-many relationships between instances
of entities. You can have a model like this:
entity Category {
string attribute name;
to-many relationship objects to Object, inverse category;
};
entity Collection {
string attribute name;
to-many relationship objects to Object, inverse collections;
};
entity Object {
string attribute name;
to-one relationship category to Category, inverse objects;
to-many relationship collections to Collection, inverse objects;
};
When working with Core Data you do not need to create or maintain
join tables to represent many-to-many relationships, unlike SQL. You
can simply manipulate them directly either in your interface via
NSArrayController or programmatically by interacting with the sets
returned by -mutableSetValueForKey: and Core Data will do the right
thing with respect to inverse management, foreign key maintenance in
persistent stores that require it, etc.
One thing to keep in mind when building interfaces to related
entities that use Cocoa bindings is that if you want to show the
contents of a to-many relationship, you'll need to add an additional
array controller to represent the relationship contents. The new
array controller needs to be set up to reference the entity the
related objects are instances of, and have its managedObjectContext
binding set to the same context as the relationship source's array
controller. It also needs to have its contentSet binding set to the
controller key and key path of the relationship it is intended to
represent. Without this step, the array controller will represent
all instances (or all instances as filtered by a predicate) of the
specified entity.
-- Chris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden