Re: CoreData: Can't modify an immutable model
-
Chris,> I am trying to create a core data entity using the method
> enittyForName: inManagedObjectContext .
You want an NSManagedObject, not an NSEntityDescription. Each
managed object *has* an entity description. The entity description
defines the object's properties and caches.
NSManagedObject is to NSEntityDescription what id is to Class
Or for a database analogy, the entity describes the table, and the
managed object describes the row.> No, or maybe only for a few microseconds. I was kind of confused by
> this the first time that I used Core Data...
> First, you "insert".
> Then, you modify.
> Seems backwards, but it works. You don't "create" a managed object.
Yes. If you really want to, you can pass nil in for the
NSManagedObjectContext* parameter, and call -insertObject: later.
But there's a limit to how much you can do with a managed object if
it is not registered with a context. For example, inverse
relationship maintenance won't work so you can't set any
relationships.
--
-Ben -
On 2/5/08 1:50 PM, Ben Trumbull said:>> No, or maybe only for a few microseconds. I was kind of confused by
>> this the first time that I used Core Data...
>> First, you "insert".
>> Then, you modify.
>> Seems backwards, but it works. You don't "create" a managed object.
>
> Yes. If you really want to, you can pass nil in for the
> NSManagedObjectContext* parameter, and call -insertObject: later.
> But there's a limit to how much you can do with a managed object if
> it is not registered with a context. For example, inverse
> relationship maintenance won't work so you can't set any
> relationships.
Ben,
Are you talking about insertNewObject
ForEntityForName:inManagedObjectContext: ? Do I understand correctly
that the moc parameter is in fact optional? The docs don't say so, but
I have always wished it was the case. Can you confirm? (I'd like to be
able to set some attributes before NSObjectControllers go changing the UI).
Thanks,
--
____________________________________________________________
Sean McBride, B. Eng <sean...>
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada -
At 5:04 PM -0500 2/5/08, Sean McBride wrote:> Ben,
>
> Are you talking about insertNewObject
> ForEntityForName:inManagedObjectContext: ? Do I understand correctly
> that the moc parameter is in fact optional? The docs don't say so, but
> I have always wished it was the case. Can you confirm? (I'd like to be
> able to set some attributes before NSObjectControllers go changing the UI).
Yes it is optional, but it's not for the faint of heart. You won't
get relationship maintenance, undo, or the standard notifications
until you call -insertObject:
This is documented under NSManagedObject's designated initializer,
which the convenience method on NSEntityDescription eventually
invokes.
Incidentally, on 10.5, if you're working with a large batch of new
objects, it's a lot faster to look up the entity description once and
use:
MySubclass* mo = (MySubclass*)[[NSManagedObject alloc]
initWithEntity:entity insertIntoManagedObjectContext:moc];
This works as NSManagedObject's +alloc always allocates the correct
Class for the entity you pass into the initializer. Effectively,
NSManagedObject is a class cluster for the subclasses in your model.
On Tiger, you'd have to do that yourself.
--
-Ben


