Re: CoreData questions: How to reset NSManagedObject, how to "Un-manage" an NSManagedObject.
Re: CoreData questions: How to reset NSManagedObject, how to "Un-manage" an NSManagedObject.
- Subject: Re: CoreData questions: How to reset NSManagedObject, how to "Un-manage" an NSManagedObject.
- From: Andreas Grosam <email@hidden>
- Date: Wed, 23 Mar 2011 13:05:59 +0100
On Mar 23, 2011, at 8:21 AM, Motti Shneor wrote:
> Hi.
>
> 1. When you create an NSManagedObject, it is initialized with default values defined in the model. How can I reset an existing NSManagedObject to these default values?
Unless there is a better way, I would suggest to use a dictionary representation of a freshly created managed object through taking a "snap-shot" of this object using dictionaryWithValuesForKeys:. Then "reset" any managed object by sending it setValuesForKeysWithDictionary:. You may declare this dictionary as a class variable of your custom class and initialize it once when you create the first managed object. You may get the key paths using the entity description.
>
> 2. We have a multithreaded application, and we only keep one core-data context. Our network-related code receives data in a background thread, but is unable to write it to the model directly. So it saves it in some intermediate data object, and passes it to the main-thread for writing into the model.
> I would like to use an NSManagedObject to replace the intermediate data object --- It is quite ugly to have duplicated model classes definition for everything. My question:
I wonder why you cannot modify the model (that is save a context) within the background thread. Business logic should be honored regardless of the thread. So, usually you would do the following:
Use one dedicated context for each thread. Your background thread imports new objects and inserts (or modifies) it into its context, say "import context". A controller has registered (e.g. on the main thread) for context save notifications (NSManagedObjectContextDidSaveNotification) sent from import context. It also specifies a method to be invoked, say importContextDidSave::
// controller.m
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(importContextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:importContext];
When you are ready with importing, save the import context in the background thread (or do it periodically if this is appropriate). When the context is saved, the notification center invokes the registered method in your controller. Upon receiving, you merge the import context with your main context (which may be used for a table view), so basically:
- (void) importContextDidSave:(NSNotification*)saveNotification {
[mainContext mergeChangesFromContextDidSaveNotification:saveNotification];
}
Here, the current thread-context is irrelevant as long as you do not access the managed objects directly which are accessible through the userInfo of the saveNotification. Using mergeChangesFromContextDidSaveNotification:, the context is able to safely merge the changes, regardless of the thread-context. Note, that due to merging, the mainContext's state with respect to hasChanges does not change.
Please note, that this background thread does not necessarily have to be the same thread where you import data. It can be any "worker thread", and it may take any imported data objects from a fifo which are subsequently taken to create managed objects. Also, this worker thread may take any actions to perform additional validations or plausibility checks which may have prevented the import thread to save these objects to the model.
Fortunately, there is quite good documentation for threading and Core Data. "Core Data Programming Guide", "Concurrency with Core Data".
There are also good examples for this. Please take look. ;)
>
> Can I somehow detach an NSManagedObject from its context, and use it in an "Unmanaged" way?
No, a managed object must be registered with a managed object context.
> Hopefully I can later attach it to the context, or create another NSManagedOBject from its data?
You could use a dictionary representation and initialize a managed object from it.
Regards
Andreas
>
>
> Motti Shneor,
> Spectrum Reflections LTD.
> ---
> ceterum censeo microsoftiem delendam esse
> ---
_______________________________________________
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