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: Motti Shneor <email@hidden>
- Date: Wed, 23 Mar 2011 21:49:15 +0200
- Resent-date: Thu, 24 Mar 2011 20:50:49 +0200
- Resent-from: Motti Shneor <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: Cocoa-Dev List <email@hidden>
Thanks Andreas, for such a complete and helpful answer.
Regarding reset issue, I was curious --- after all, NSManagedObject gets its initial dictionary from the Model definition somehow, doesn't it? why can't I do it, or even better, why isn't it implemented as a NSManagedObject API?
I find resetting an object a very basic functionality. I'll see how I can write some generic code to do it for all my entities.
Regarding the other issue.... (sigh)
Well, we are contractors for a big company. They have their old server-client code modules, and they try to keep them cross-platform (Yup, Windows). That's why they refrain from any Core-Data direct calls in the background thread client code. It's not because it's on a thread, it's because this code is not "Mac only" code.
We DO employ the technique you demonstrated, (a flavor of it) and we do have 2 contexts (one writing, one reading) to achieve transactional behavior on the UI side, (updating the UI on controllable sets of changes).
The big-company guys, simply duplicated all the model classes generated from the model, polluting the namespace, and introducing pitfalls all over the place (If the model changes, it is no longer in sync with those data objects). I was hoping to propose a solution that will eliminate those duplicate model classes.
Thanks again!
Motti.
On 23/03/2011, at 20:27, Andreas Grosam wrote:
> 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:
>
>>
>> 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