toast toast - 2 months ago 34
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

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


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