views:

21

answers:

3

Sometimes I need instantiate CoreDateEntity to store some infomations for temporarily using. But I needn't it be stored into DB.

currently I created a similar class which have same structures as the CoreDateEntity does. It works well but I have to do many datas transfer between Two models.

Is there any better way to handle this?


Thanks for all the replies. but you guys just give me half answer of this. consider about this, I need place some entity without MOC into current database pool, how could I do this? I already checked the documents of CoreData, seems I didn't find API to transfer one entity from MOC to another MOC(manage object context).

+2  A: 

You can assign entities to different stores when you set up the data model. Have one store be the persistent store and the other an in-memory store. You can't form relationships across stores but it sounds like you don't need that.

To assign a configuration, hit the configuration tab (the one with the wrench icon) in the entity detail (where you give it its name, class and parent). When you create the persistent store, add the configuration name to the options dictionary.


Update:

I think you maybe overcomplicating things. It sounds like you have some managed objects that will be temporary and some that will persisted but sometimes you may want to save the temporary objects. I don't think you should bother trying to separate out the "temporary" objects. Doing so just adds complexity without any performance benefit. Instead, just use ordinary persisted objects and then delete the ones you don't want.

Always try the simplest solution first.

TechZen
but how could I transfer some entity's MOC to another so it could be stored in the persistent store?
xhan
You could not persist an entity assigned to the in-memory store. You would have to clone it to an entity assigned to the persistent store. I'm not really clear about what you want to do.
TechZen
A: 

Use two different managed object context's and only save the objects from one context. Be careful not to set relationships between objects of two different context's - this doesn't work.

brutella
A: 

According to Apple docs you can initialize a managed object without context if you specify nil as context.

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context
chance
good tip. but what if I need the instance be insert into db pool later? how could I do it?
xhan
if you later want to register the object with a particular context you just call [context insertObject:managedObject]. That's the same method actually that gets called by initWithEntity... if the context is not nil.
chance