简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM