WatchOS2 WCSession sendMessage doesn't wake iPhone on background

后端 未结 3 1351
悲&欢浪女
悲&欢浪女 2021-01-04 04:17

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

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 04:51

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

    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.

    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.

    Final solution:

    - (void)session:(WCSession *)session
        didReceiveMessage:(NSDictionary *)message
             replyHandler:(void (^)(NSDictionary *_Nonnull))replyHandler {
    
        replyHandler(@{ @"schedule" : @"OK" });
    
        dispatch_async(dispatch_get_main_queue(), ^{
          Nested RXPromise calls.....
        });
    
    }
    

提交回复
热议问题