WatchOS2 WCSession sendMessage不会在后台唤醒iPhone [英] WatchOS2 WCSession sendMessage doesn't wake iPhone on background

查看:180
本文介绍了WatchOS2 WCSession sendMessage不会在后台唤醒iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已在Simulator和实际物理设备iphone5s上进行了测试.我试图使用WCSession sendMessage从WatchOS2扩展名传输到iPhone iOS9代码.当iPhone应用程序在前景和后台模式下运行时,它可以很好地工作.

This is being tested on both Simulator and real physical device iphone5s. I tried to use WCSession sendMessage to communicate from WatchOS2 extension to iPhone iOS9 code. It works well when iphone app is running either in the foreground and background mode.

但是,如果我终止了iPhone应用程序(根本不运行应用程序),那么我总是会遇到errorHandler超时.因此,Watch无法再与iPhone通信.

But If I kill the iPhone app (not running app at all), then I always got errorHandler timeout. So Watch cannot communicate with iPhone anymore.

错误域= WCErrorDomain代码= 7012"消息回复花费的时间太长. UserInfo = {NSLocalizedDescription =邮件回复花了太长时间., NSLocalizedFailureReason =发生回复超时.}.

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

我认为它应该在后台唤醒iPhone应用程序.

I think it supposed to wake iPhone app in the background.

有什么办法解决此问题或解决该问题吗?谢谢!

Any idea what to work around this problem or fix it? Thank you!

推荐答案

经过数小时的尝试和来自@jeron的提示.我终于自己解决了这个问题.

After hours of trying and hint from @jeron. I finally figured out the problem myself.

在我的session:didReceiveMessage委托方法中,我有两个调用. 1.replyHandler调用. 2.在我的情况下,我正在运行一个异步进程(RXPromise),它嵌套了许多RXPromise回调,以从云服务中获取各种数据.我没有注意它,因为它应该立即调用并返回.但是,现在我一起注释掉了RXPromise块,它可以每次在后台唤醒iOS应用.

In my session:didReceiveMessage delegate method, I have two calls. 1.replyHandler call. 2. I have an async process running (RXPromise) in my case, It nested quite a few RXPromise callbacks to fetch various data from cloud service. I didn't pay attention to it, because it is supposed to call and return right away. But now that I commented out RXPromise block all together, it can wake up iOS app in the background every time.

最后,我发现造成麻烦的原因是,在调用RXPromise之后,不再保证可以重新回到主线程.而且我相信session:didReceiveMessage必须在主线程上返回.我在Apple文档中的任何地方都没有看到这一点.

Finally I figure out the trouble make is because after RXPromise call, it is not guaranty to be landed back to main thread anymore. And I believe session:didReceiveMessage has to be return on the main thread. I didn't see this mentioned anywhere on the Apple documentation.

最终解决方案:

- (void)session:(WCSession *)session
    didReceiveMessage:(NSDictionary<NSString *, id> *)message
         replyHandler:(void (^)(NSDictionary<NSString *, id> *_Nonnull))replyHandler {

    replyHandler(@{ @"schedule" : @"OK" });

    dispatch_async(dispatch_get_main_queue(), ^{
      Nested RXPromise calls.....
    });

}

这篇关于WatchOS2 WCSession sendMessage不会在后台唤醒iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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