我正在使用 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/