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
2010-08-05 23:23:36
No I didn't initialize the managed object manually.
Riq Hopkins
2010-08-05 23:25:54
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
2010-08-05 23:37:33
You are right TechZen, the object was mistakenly given a nil managedObjectContext at initialization. Thanks :)
Riq Hopkins
2010-08-05 23:55:33