views:

16

answers:

1

In my code, I am trying to access [NSManagedObject managedObjectContext] but surprisingly it returns nil. The managed object is loaded because I already successfully accessed it's values using [NSManagedObject valueForKey:]. Is there any case where managedObjectContext returns nil?

+1  A: 

If you initialize a managed object directly using init without inserting it into a context, then it may have a nil context. If you insert it and pass a nil value for the context, it will have a nil context.

TechZen
No I didn't initialize the managed object manually.
Riq Hopkins
Can't say without seeing code. The only way for it to have a nil context is to never give it one. I don't even think you can set it nil once you've assigned it to one. How are you creating the managed object?
TechZen
You are right TechZen, the object was mistakenly given a nil managedObjectContext at initialization. Thanks :)
Riq Hopkins