CoreData: "Can't reassign an object to a different store once it has been saved."?
CoreData: "Can't reassign an object to a different store once it has been saved."?
- Subject: CoreData: "Can't reassign an object to a different store once it has been saved."?
- From: Jim Correia <email@hidden>
- Date: Tue, 13 Dec 2005 12:35:05 -0500
I have two stores - a persistent store and an in memory store. (The
in memory store is used for things like synthetic smart groups that
should never be saved to disk - similar to how CoreRecipes is modeled.)
Right now I'm explicitly assigning objects to the stores by hand with
- (void)assignObject:(id)object toPersistentStore:(id)store
The docs say "It is only necessary to use this method if the
receiver’s persistent store coordinator manages multiple writable
stores that have object’s entity in their configuration. Maintaining
configurations in the managed object model can eliminate the need for
invoking this method directly in many situations. If the receiver’s
persistent store coordinator manages only a single writable store, or
if only one store has object’s entity in its model, object will
automatically be assigned to that store."
I've tried creating two configurations - InMemoryConfig and
PersistentConfig and use them when creating the stores. However, if I
create a new object (which will end up in the persistent), and give
it a relationship to an already saved object in the persistent store,
I get "Can't reassign an object to a different store once it has been
saved." when saving. Paradoxically, it looks like this happens when
it is trying to assign the object a persistent store equal to its
existing persistent store.
Am I obviously doing something wrong? I can try to come up with a
reduced standalone example if necessary.
If I have to continue assigning them by hand, I did notice this in
the documentation:
"In many applications, there is no need to subsequently assign a
newly-created managed object to a particular store—see
assignObject:toPersistentStore:. If your application has multiple
stores and you do need to assign an object to a specific store, you
should not do so in a managed object's initializer method. Such an
assignment is controller- not model-level logic."
I agree with what the documentation says in principle. However, in
practice if I have a custom subclass of NSManagedObject which is
always (or nearly always) going to exist only in the persistent
store, from maintenance point of view, it seems better to do the
assignment once in awakeFromInsert rather than in every controller
through the app which may make one of these. Besides going against
the grain of the doc, is there anything wrong with this? It seems
wrong to cut and paste the assignment everywhere else just because it
ought to be controller logic. Is there a more appropriate pattern?
Thanks,
Jim
_______________________________________________
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