1

I'm trying to learn the basics of core data and have started a single view application whilst having a basic template of the master-detail application with core data open for reference.

I'm stuck trying to set my managedObjectContext within my MasterViewController via AppDelegate.swift.

I have this so far, the master-detail template uses a split view which I'm not using, so how can I do a similar thing with only a single-view application?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // How to reference MasterViewController???

    controller.managedObjectContext = self.managedObjectContext
    return true
}

2 Answers 2

2

You can access the managed object context all over the app using the following line of code.

let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

So you can remove the following line.

controller.managedObjectContext = self.managedObjectContext
Sign up to request clarification or add additional context in comments.

2 Comments

Okay, cool so I've got just that line in my application() function in AppDelegate.swift So how do I access that in, say my MasterViewController?
For convenience, you can add a static property into AppDelegate static var mObjectContext: NSManagedObjectContext {return (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext}
2

I'd recommend passing the NSManagedObjectContext to the appropriate view controller/s as recommended in the Apple documents. This involves the following steps:

  • prepare your Core Data stack;
  • retain a strong reference to the NSManagedObjectContext prepared in the Core Data stack;
  • include an NSManagedObjectContext public property in your (swift) view controller file;
  • set that public property in the view controller from the reference held in the stack.

From the Apple Documentation...

Getting a Managed Object Context

In iOS:

By convention, you get a context from a view controller. You must implement your application appropriately, though, to follow this pattern.

When you implement a view controller that integrates with Core Data, you can add an NSManagedObjectContext property.

When you create a view controller, you pass it the context it should use. You pass an existing context, or (in a situation where you want the new controller to manage a discrete set of edits) a new context that you create for it. It’s typically the responsibility of the application delegate to create a context to pass to the first view controller that’s displayed.

A view controller typically shouldn’t retrieve the context from a global object such as the application delegate—this makes the application architecture rigid. Neither should a view controller create a context for its own use (unless it’s a nested context). This may mean that operations performed using the controller’s context aren’t registered with other contexts, so different view controllers will have different perspectives on the data.

Sometimes, though, it’s easier or more appropriate to retrieve the context from somewhere other than application or the document, or the view controller. Several objects you might use in a Core Data-based application keep a reference to a managed object context. A managed object itself has a reference to its own context, as do the various controller objects that support Core Data such as array and object controllers (NSArrayController and NSObjectController in OS X, and NSFetchedResultsController in iOS).

Retrieving the context from one of these objects has the advantage that if you re-architect your application, for example to make use of multiple contexts, your code is likely to remain valid. For example, if you have a managed object, and you want to create a new managed object that will be related to it, you can ask original object for its managed object context and create the new object using that. This will ensure that the new object you create is in the same context as the original.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.