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: Quincey Morris <email@hidden>
- Date: Sun, 23 Aug 2009 21:45:28 -0700
On Aug 23, 2009, at 17:32, Dieterich Lawson wrote:
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.
Actually, although the internally-generated accessors are more
efficient than the ones you could write, the data is stored in the
same way.
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.
If all such objects are transient (not to be saved), then use a
transient property.
If only some of the objects are transient, create them in the managed
context using insertNewObjectForEntityForName:inManagedObjectContext:,
then immediately delete them. The documentation perhaps doesn't spell
this out, but the "inserting" and "deleting" have reference to the
persistent store, not the managed object context. That is, "deleting"
a managed object doesn't cause the in-memory object to be deallocated,
it causes it to be removed from the store at the next save. (Or, in
this case, not ever to be saved.)
"Deleted" objects survive as long as they are owned by something.
Since managed object contexts by default do not retain managed objects
(except inserted or updated ones), that means the deleted objects will
survive as long as your code maintains ownership (i.e. retains them).
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.
Personally, I'd use a transient property to hold *all* the downloaded
story objects, plus a non-transient property to hold the ones that
need to be saved. Obviously you'd need a "feed" entity (or something
along that line) for these to be properties of.
HTH
_______________________________________________
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