Why send a message from the WatchKit extension on iOS and get a response so slowly?

I use the sendMessage method to send a message from the WatchKit extension to an iOS application. A response time of 230 ms is required on average . The time does not depend on whether the iOS application is running on the screen or running in the background. 230 ms is approximately the time it takes for light to travel around the circumference of the Earth and back. But the phone sits at a distance of 30 cm from my watch when I test it.

Questions

  • Why is it so slow?
  • Is it supposed to be so slow?
  • Is there any way to do this faster?

Observation: according to my previous experiments in watchOS 1, the connection was a little faster, the feedback was used to get about 50 ms.

Send a message from the WatchKit extension

 let session = WCSession.defaultSession() session.sendMessage(["message from watch":"๐ŸŒท"], replyHandler: { reply in // Getting reply from iOS app here }, errorHandler: nil) 

Receive message from iOS app

 func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) { replyHandler(["reply from iOS":"๐Ÿฑ"]) } 

Demo app: https://github.com/evgenyneu/WatchKitParentAppBenchmark

iOS: 9.0, watchOS: 2.0

+5
source share
2 answers

AFAIK, when you send a message to another device, the message will be archived to a file in a local directory called WatchDirectory .

This directory will be synchronized with another device, for example, with another iCloud Drive application or Drop Box via Bluetooth. Since this approach does not require an application that works for iOS and watchOS until the transfer is complete.

When new files have been sent to the directory, iOS (or watchOS) will call the WCSession API associated with it to process the content. If necessary, iOS (or watchOS) will wake the recipient application in the background before sending the message.

With watchOS1, the watch extension works on iOS, only the remote user interface works on AppleWatch. Therefore, this requires a much simpler process of communication, but simply the connection between the processes.

sendMessage much more expensive than other communication APIs provided by WCSession . iOS cannot use it as long as the applicationโ€™s application is in the foreground, and using sendMessage from watchOS should wake the iPhone and launch the iOS application in the background. After processing sent messages, iOS can kill the target application running in the background to recover memory.

So, IMO there is no reason for this to be fast.

+2
source

In my case, to instantly update my interface on the device:

  func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) { //receive message from watch dispatch_async(dispatch_get_main_queue()) { self.textLabel.text = message["optionSent"]! as? String } } 
0
source

All Articles