For what it's worth, Marcus Zarra seems to be promoting the nil
context approach, claiming that it's expensive to create a new context. For more details, see this answer to a similar question.
Update
I'm currently using the nil context approach and have encountered something that might be of interest to others. To create a managed object without a context, you use the initWithEntity:insertIntoManagedObjectContext:
method of NSManagedObject
. According to Apple's documentation for this method:
If context
is not nil
, this method
invokes [context insertObject:self]
(which causes awakeFromInsert
to be
invoked).
The implication here is important. Using a nil
context when creating a managed object will prevent insertObject:
from being called and therefore prevent awakeFromInsert
from being called. Consequently, any object initialization or setting of default property values done in awakeFromInsert
will not happen automatically when using a nil
context.
Bottom line: When using a managed object without a context, awakeFromInsert
will not be called automatically and you may need extra code to compensate.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…