How to share data using Watch Connectivity when working with Core Data

馋奶兔 提交于 2019-12-03 06:02:34
  • How to share the array of Objects with the Watch OS Extension ? Since you are using WatchConnectivity framework, send the array of objects from iPhone using sendMessage method and in your FavoritesInterfaceController implement the func session(session: WCSession, didReceiveMessage method in order to get the response or you can get the array in replyhandler to.

  • If the user adds/edits/deletes objects in the array on the Watch OS how can we update the data on the iPhone ?

    Send the objectId along the with new changes in your sendMessage method from watch to phone, on receiving on phone made the changes in your database save it and send the updated value in your replyHandler so that your watch content will be updated accordingly.

  • Also the iOS application is embedded within a UITabBarController so does it matter which view controller I communicate with ?

    You desired viewController to which you are communicating OR the one that is responsible for doing changes should be alive. If multiple ViewControllers are listening to WCSessionDelegates then when you send any message from watch all of the live controllers will receive that message. You should include some kind of identifier in your sendMessage dictionary so you can know which operation to perform. Like if you want to delete an object then when watch sends a message the identifier will contain delete so that on receiving you can check the identifier value and perform the delete operation.

You can use the replyHandler in the sendMessage to do this. Make sure you implement the reply handler on both Watch and iOS App to get this.

Basically, if you get it right, your reply handler can ensure what your iOS app does in response for a watch app's message.

Also, speaking of your response (of sending an array of objects) - you should send it as a dictionary and fetch it on the watch.

First of, this is a really good question. For starters I'd recommend that you watch this session from the WWDC 2015: Session 713 - Introducing Watch Connectivity. This can be found here.

Now to your actual question. There is a great tutorial and Github repo that show you how to communicate Core Data between your Apple Watch app and the container app using App Groups, as this enables you to access all shared content, such as Core Data and even NSUSerdefaults.

You can then find the complete tutorial on how to do this under the following link.

Hope that helps, Julian.

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