re: Using Core Data NSManagedObject subclasses outside of a managed object context
re: Using Core Data NSManagedObject subclasses outside of a managed object context
- Subject: re: Using Core Data NSManagedObject subclasses outside of a managed object context
- From: Ben Trumbull <email@hidden>
- Date: Mon, 24 Aug 2009 14:22:34 -0700
I have objects that I use with core data that were automatically
generated
for me by xcode and represent entities in my data store. They all
subclass
NSManagedObject, and do not have instance variables, but instead use
the
@dynamic setting for their properties, pretty standard. My
understanding is
that this allows the NSManagedObject superclass to generate the
getters/setters at runtime and store data in it's own, more
efficient, Core
Data friendly way.
yup
What this means for me, however, is that I can't just call [[Entity
alloc]
init] and then get/set Entity's properties, because it won't properly
initialize unless it is given a managed object context. I need to be
able to
use my objects in places that they won't be persisted, as just
transient
objects, but this prevents that. The only way I know to initialize
them is
by calling [NSEntityDescription insertNewObjectForEntityForName:name
inManagedObjectContext:managedObjectContext] . But, when creating
objects
this way, they will be persisted on the next save call.
There's no requirement that you put them in the same
NSManagedObjectContext that you call save: on. You can just create a
second transient MOC. Alternatively, you can add an in-memory store
to your NSPersistentStoreCoordinator and assign the newly inserted
objects to that store with -assignObject:toPersistentStore: and then
they'll get "saved" to an in memory backing store. This is probably
easier to manage, and definitely provides more features than the
second MOC approach. It does do a touch more work, though.
Just so you know, I'm making a feed reader that has the option to save
selected stories from the feed for later, offline, browsing. I want
to be
able to download the feed XML and create 'story' objects from that
without
having to persist every story I fetch.
Regardless of approach, you'll need to copy the transient objects into
new persistent objects to individually change their nature from
transient to normal managed objects.
- Ben
_______________________________________________
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