toast toast - 10 days ago 6
Swift Question

How do I add Core Data stack to WatchKit app

How do I add my core data stack (from my iOS app) to my WatchKit app? In my iOS app, the Core Data stack is in my

AppDelegate
, however there is no
AppDelegate
in my WatchKit app. Does this mean I need to create a new
AppDelegate
file for the Watch app?

Answer

You could put the stack in a singleton. Create a new class make it a singleton so you know you will only ever have one instance.

    class CoreDataStack: NSObject {
    // MARK: - Singleton with private init
    static let sharedInstance = CoreDataStack()
    private override init(){
    }
    // add your core data stack here
}

Then you can access it from any where in your app by simply

 let coreDataStack = CoreDataStack.sharedInstance
//and then you can do things like
let myManagedObjectContext = coreDataStack.managedObjectContext