ios - 多次调用 WCSession sendMessage errorHandler

标签 ios swift watchkit sendmessage watchconnectivity

我正在使用 WatchKit 2.0,我注意到一个非常奇怪的行为。如果我使用我的 watch 应用程序超过 5 分钟,我开始从 WCSession sendMessage 调用中收到超时错误,该调用之前已被调用并成功完成。我将错误打印为:

Error Domain=WCErrorDomain Code=7012 "Message reply took too long." UserInfo={NSLocalizedDescription=Message reply took too long., NSLocalizedFailureReason=Reply timeout occured.}

我仍然可以在不同的 Interface Controllers 中进行其他调用,但我一直在 Interface Controllers 中调用 errorHandlers 已关闭(使用顶部的后退按钮)。

有谁知道可能导致这种行为的原因是什么?我没有在代码中结合分层和基于页面的界面样式,并且在使用该应用程序的前 5 分钟内一切正常。

更新

代码如下:

    if WCSession.isSupported() {
        // Set the session to default session singleton
        let session = WCSession.defaultSession()

        // Fire the message to iPhone app
        session.sendMessage(["action": "getProfile", "memberId": citizen!.memberId], replyHandler: { (response) -> Void in


            if response["messageData"] != nil {
                // There is data in the reply
                let jsonObject = JSON(response["messageData"]!)

                ...

                // Display the profile details
                self.displayProfileDetails()



            } else if response["error"] != nil {
                // Get the error message and display it
                self.showAlert(nil, message: WatchUtils.getErrorString(response["error"]), action: .GET_PROFILE)
            }
            }, errorHandler: { (error) -> Void in
                print("error: \(error)")
                // Show alert
                self.showAlert(nil, message: NSLocalizedString("watch_connectivity_error", comment: "Watch can't connect to phone"), action: .GET_PROFILE)
        })
    }

replyHandler 最初被调用,但由于某种原因,5 分钟后 errorHandler 被调用,并且每隔几秒就会被调用一次。

最佳答案

几个月前我遇到了同样的问题。我想出的解决方案是使用一个变量来存储我为上次发出的请求提供的 ID,例如

self.lastCallId = "watch_load_clients"

如果没有收到匹配的 ID(我也将 ID 发回),那么我将忽略该请求。

我认为这只是蓝牙问题。

编辑:另外,您是否提出请求但未收到回复?当您的问题发生时就是这种情况。

关于ios - 多次调用 WCSession sendMessage errorHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36365260/

相关文章:

ios - 在 WatchKit 中使用应用内购买功能来获得一目了然的功能和复杂功能

ios - 自动续订购买的应用程序拒绝 - 准则 3.1.2 - 业务 - 付款 - 订阅

swift - NSCollectionViewItem 的圆角

ios - 如何将按钮添加到 UITableView 页脚 View 但保留最后一个单元格分隔符?

ios - 后台任务不在 Swift 中调用类

ios - 作为 willActivate 的一部分更新界面时的奇怪行为

iphone - 如何计算减速结束后 ScrollView 将停止的 contentOffset?

ios - 由于地理限制的应用程序不断被苹果拒绝。我应该怎么办?

ios - 是否可以从电话簿中检索我自己的联系方式?

ios - 快速通过 UITextField 的 UILongPressGestureRecognizer