iosdude
iosdude

Reputation: 1139

Core Data properties that are cleared on app exit (or not save to persistent store)

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?

Upvotes: 0

Views: 62

Answers (2)

Tom Harrington
Tom Harrington

Reputation: 70976

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.

Upvotes: 0

Jon Rose
Jon Rose

Reputation: 8563

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.

Upvotes: 0

Related Questions