berec
berec

Reputation: 785

CoreData: Using transient properties with read-only persistent store

Dealing with managed objects (NSManagedObject) from read-only persistent store I was trying to use transient properties to store some temporary values. Took in account transient properties were not saved to persistent store I supposed there was nothing wrong to use them for cache purposes. But as it turned out you cannot write data even in transient properties of managed objects from read-only store. During NSManagedContext's save operation i got this error

"Cannot update objects into a read only store"

(I'm sure only transient properties were changed.)

Why is that? Can it be considered as a NSManagedObjectContext's bug? Thanks for sharing your ideas.

Upvotes: 0

Views: 365

Answers (2)

bteapot
bteapot

Reputation: 2027

You can declare your own property in entity class:

@interface DBExample : NSManagedObject

@property (nonatomic, strong) NSDictionary *userInfo;

@end

Implementation:

@implementation DBExample

@synthesize userInfo = _userInfo;

@end

And by the way, why are you saving context with attached read-only persistent store?

Upvotes: 1

Mundi
Mundi

Reputation: 80273

This is expected behavior. A read only store cannot be modified. Not even in the managed object context (in memory). That is the meaning of "read only". Hardly a bug.

The solution is fairly simple. Create a second in-memory persistent store and integrate it into your managed object model via Configurations. Keep track of your transient property via this store. Perhaps you have to create a "wrapper" entity and link it to the read-only store via a relationship.

Despite the effort of creating a more complicated model setup, I think this is a feasible solution because once you have done this setup you can basically forget about it.

Upvotes: 2

Related Questions