问题
I am pretty new to WatchKit, and I am trying to add Core Data to my WatchKit extension. So far I have created a Data Model in my WatchKit extension, but I am stuck on implementing the rest of the Core Data stack.
Is the process of creating Core Data models and initializing the Core Data stack identical to iOS or are there any differences I should be aware of?
Also, do I initialize the Core Data stack in the ExtensionDelgate.swift
or should I initialize it somewhere else?
回答1:
The process of setting up the watchOS Core Data stack is identical to iOS. In fact, you can share that common code (and even a common data model) between your iOS and watchOS targets.
You can initialize your stack when your application launches, or move that code out of the (app or) extension delegate into a shared manager, which gets initialized the first time you need to obtain data from your model.
I actually wait until my interface controller's awakeWithContext
to load my data (which lazily initializes my manager's stack).
If you're looking for a working example, Jesse Squires has an excellent Swift Core Data stack which you might find helpful.
来源:https://stackoverflow.com/questions/36096785/implementing-core-data-to-watchos-2-0