redeyes
redeyes

Reputation: 183

Swift, storyboards and core data...a missing persistentstorecoordinator?

I am working on a new Cocoa project using Swift, Core Data and storyboards, and have come across a problem that makes no sense to me. After some fairly extensive hunting around, including on this site, I have come to the conclusion that I must be missing something obvious, but cannot figure out what. Here is what I have done so far:

1.Create a new project, Cocoa Application, using Swift, Storyboards, and Core Data. 2. Create an entity in the .xcdatamodeld. Let’s call it Dataset. 3. Create a subclass of NSSplitViewController (for what i want to do in the rest of the program). 4. Set the window content of the main window to and instance of myVC. I checked, and it loads up and displays fine. 5. In the viewController.swift, get the managedObjectContext like so:

@IBOutlet var moc:NSManagedObjectContext!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.

    let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate

   moc = appDelegate.managedObjectContext


    println("mainsplitviewcontroller moc:")
    println(moc)
    println("mainsplitviewcontroller psc:")
    println(moc.persistentStoreCoordinator)




     NSLog("Main split view loaded")
}

(yes, I have about dependency injection, but I want to solve this problem first).

  1. In IB, put a managedObjectContext object in the View Controller instance.

  2. In IB, connect the myVC outlet for the variable moc to the managedObjectContext.

  3. In IB, create an array controller. Set it To Entity. Entity Name is Dataset. Turn on Prepares Content.

  4. Either as an outlet or a binding, connect the array controller to the MOC. Using outlet, just dragging from managed object context in it's right-click popup to the icon for the MOC created in 6 above. For bindings, the old fashioned way, going to the bindings tab, and under Parameters, Bind to: (view controller), Model Key Path: moc. (moc is from 5 above)

Then, I build and run. and I get the error: "Cannot perform operation since managed object context has no persistent store coordinator."

This happens whichever way I try to do 9, above.

Now, the thing is, from my println statements, the objects referred to in both the app delegate and the viewcontroller are the same, both for the managed object context and for the persistent store controller, as below:

appdelegate moc: appdelegate psc: mainsplitviewcontroller moc: mainsplitviewcontroller psc:

I wish I could show images, but I am new here and so cannot do that. Am I doing something clearly wrong? I thought I understood the process: make sure the VC can access the MOC, then put the MOC object into the VC's window in IB, make it an outlet, and connect it to an array controller. Why would the swift file for the view controller seem to show that the PSC is the same as the app delegate, but in IB, the array controller think the MOC has no PSC at all?

Thanks for reading!

Upvotes: 1

Views: 985

Answers (1)

Jenny
Jenny

Reputation: 21

I don't know if this is going to help, but I'm not surprised that your project shows that error. You have two managed object contexts - one created by your app delegate, and one created by the storyboard. Your interface code is connecting to that second MOC, which is not connected to your persistent store.

Upvotes: 2

Related Questions