Question

I have a NSManagedObject class for which I have added a custom instance variable, by making it a property and using @synthesize (but not using 'transient' in the Core Data model). This works fine if I set the value and read it later within the same thread; but if I set the value for this property in a different thread, it still shows nil on the main thread.

As an example, think of a this:

@interface Person : NSManagedObject{

}
@property (nonatomic, strong) UIImage *personImage; // unmodeled

If I set the personImage object in the background thread, it's still 'nil' on the main thread.

Is there a way to fix this so that the property actually stores the value across threads?

Was it helpful?

Solution

There is no way to "fix" it because it is not broken. Each context (and thus each thread) will have a separate instance of the NSManagedObject. They are completely different objects in memory and will not share instance variables. You will need to initialize that instance variable in each instance.

That is why awakeFromInsert and awakeFromFetch fire on each context. It gives you an opportunity to initialize those instance variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top