使用Core Data时如何使用Watch Connectivity共享数据 [英] How to share data using Watch Connectivity when working with Core Data

查看:104
本文介绍了使用Core Data时如何使用Watch Connectivity共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iOS应用程序中,我使用Core Data存储数据和一个获取请求来创建一个 NSManagedObject 的数组,以显示在 UITableView

In my iOS application I use Core Data to store data and a fetch request to create an array of NSManagedObjects to display in a UITableView.

在Watch OS上,我检查是否支持 WCSession 并激活会话,然后向iOS应用程序发送消息来自watchOS扩展。

On the Watch OS I check if WCSession is supported and active a session, then send the iOS application a message from the watchOS extension.

当iOS应用程序收到来自watchOS的消息时,它应该发送对象的数组在watchOS扩展中显示 WKInterfaceTable 中的数据,但我不确定如何做到这一点。最终我想要实现的是;

When the iOS application receives the message from the watchOS it should send the array of Objects to the watchOS extension to display the data in the WKInterfaceTable, but I am unsure how to do this. Ultimately what I am trying to achieve is;


  • 如何共享对象的数组使用watchOS扩展程序?

  • How to share the array of Objects with the watchOS extension?

如果用户在Watch上的数组中添加/编辑/删除对象,我们如何更新iPhone上的数据?

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

此外,iOS应用程序嵌入在 UITabBarController 中,所以我沟通的视图控制器是否重要用?

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

观看OS FavouritesInterfaceController

var session : WCSession!

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()

    //Check if session is supported and Activate
    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)
    // Interface Objects

    //Send Message
    sendmessagetoiphone()   
}

func sendMessageToIphone() {
    if(WCSession.isSupported()){
        session.sendMessage(["b":"goodBye"], replyHandler: nil, errorHandler: nil)
    }
}

IOS应用程序:FavouritesViewController

var objects = [Objects]()

func loadData() { 

    let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    let request = NSFetchRequest(entityName: "Objects")
    request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: true)]
    do {
        try
            self.objects = moc.executeFetchRequest(request) as! [Objects]
        // success ...
    } catch {
        // failure
        print("Fetch failed")
    }
 }

   func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
    //handle received message   
    let value = message["Value"] as? String
    dispatch_async(dispatch_get_main_queue()) {
        self.messageLabel.text = value
    }
    //send a reply
    replyHandler(["Value":"Hello Watch"])
   }


推荐答案


  • 如何与Watch OS Extension共享对象数组?
    因为您使用 WatchConnectivity 框架,使用 sendMessage 方法从iPhone发送对象数组,并在 FavoritesInterfaceController 中实现 func session(会话:WCSession,didReceiveMessage 方法以获取响应,或者您可以在 replyhandler 中获取数组。

    • 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.

      如果用户在Watch OS
      上添加/编辑/删除数组中的对象,我们如何更新iPhone上的数据?

      发送objectId沿着你的 sendMessage 方法中的新变化从watch到p磨练,在接到电话后,对数据库进行了更改保存,并在 replyHandler 中发送更新后的值,以便相应地更新您的观看内容。

      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.

      iOS应用程序也嵌入在UITabBarController中,所以
      与我通信的视图控制器有关系吗?

      您希望与之通信的viewController或负责进行更改的那个应该处于活动状态。如果多个ViewControllers正在侦听 WCSessionDelegates ,那么当您从watch发送任何消息时,所有实时控制器都将收到该消息。您应该在 sendMessage 字典中包含某种标识符,以便了解要执行的操作。就像你想要删除一个对象然后当 watch 发送一条消息标识符将包含 delete ,以便在收到时您可以检查标识符值并执行删除操作。

      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.

      这篇关于使用Core Data时如何使用Watch Connectivity共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆