In my opinion, it is a bad idea to directly ask another class how to get the context of a managed entity. Because the
- You cannot reuse your classes in different projects (think of an OS X application)
- You cannot receive contacts in a different context (think about using a background)
- You cannot use unit tests if a method requests another class
You must indicate this method in which context it should be selected.
Instead of + (Contact *) fetchContactWithName:(NSString *) name your method signature should look like this:
+ (Contact *)fetchContactWithName:(NSString *)name inManagedObjectContext:(NSManagedObjectContext *)context
Each viewController must have a reference to the NSManagedObjectContext used in your application. You can pass a context link to each viewController in application:didFinishLaunchingWithOptions: and each time you click or present a new viewController, you pass it a context instance.
Now this may seem like a lot of work, but some day you can take advantage of "tell me, don't ask."
Matthias bauch
source share