Create a Core Date Entity Instance But not want it to be stored(non-persistent)

前端 未结 3 679
轻奢々
轻奢々 2021-01-25 02:16

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 whi

相关标签:
3条回答
  • 2021-01-25 02:34

    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.

    0 讨论(0)
  • 2021-01-25 02:52

    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
    
    0 讨论(0)
  • 2021-01-25 02:57

    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.

    0 讨论(0)
提交回复
热议问题