0
votes

I want to store some additional attributes in my managed objects that are not saved to the persistent store together with other (persistent) properties, i.e. stored only in memory, so that after the app is restarted these non-persistent attributes are reset back to default values.

I've looked into using transient properties but they are lost when the object is fetched again from another screen, so not suitable for my case.

Does Core Data have something like this?

2
I'm not sure I completely understand your question, but sounds like in-memory store type is what you're looking for (?)Alladinian
@Alladinian I've updated my question. In short, I want to have non-persistent properties along with the persistent ones but such that they are retained when objects are re-fetched from the MOC (or another MOC).iosdude

2 Answers

0
votes

I have had good experience with storing extra values outside of core-data. I used a dictionary where the key is the id of the core-data property and the value is another mutable dictionary. This allows quick lookup for any of these extra properties. I used it in a messaging app to track currently online status.

The main downside is that all searching and sorting has to be done in-memory and not on the database level, but it was not a big problem.

0
votes

If you're using subclasses of NSManagedObject (which is usually true) then you can just add extra properties to those subclasses. There's nothing special about these properties, because not all of the properties need to be managed. Create them as you would on any other class.

You'll want to tell Xcode to use "Category/Extension" code generation for the entity instead of "Class Definition", so that you can edit the class file. But once you've done that, just add whatever properties you need. Core Data won't know or care about them.