Implementing Core Data to watchOS 2.0

拜拜、爱过 提交于 2019-12-12 10:23:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!