多次调用WCSession sendMessage errorHandler

时间:2016-04-01 20:24:15

标签: ios swift watchkit sendmessage watchconnectivity

我正在使用WatchKit 2.0,并注意到一种非常奇怪的行为。如果我使用我的观看应用程序超过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中拨打其他电话,但我一直在errorHandlers中调用已被关闭的Interface Controllers(在顶部使用后退按钮)。

有谁知道可能导致此行为的原因是什么?我没有在代码中组合分层和基于页面的界面样式,并且在使用应用程序时,所有内容都表现为前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,并且每隔几秒就会被调用一次。

1 个答案:

答案 0 :(得分:0)

几个月前我遇到了同样的问题。我想出的解决方案是使用变量来存储我给出的最后一个请求的id,例如

self.lastCallId =“watch_load_clients”

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

我认为这只是一个蓝牙问题。

编辑:此外,您是在提出请求还是没有收到回复?当你的问题发生时就是这种情况。