多次调用 WCSession sendMessage errorHandler

WCSession sendMessage errorHandler called multiple times

我正在使用 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 中进行其他调用,但我不断在 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),那么我将忽略该请求。

我认为这只是蓝牙问题。

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