One weird trick to “fix” openParentApplication:reply:
(Originally published at FiveMinuteWatchKit.com, before the release of watchOS 2)
A quick perusal of the Apple Developer Forums shows more than a few developers have experienced strange bugs when calling
openParentApplication:reply: in a WKInterfaceController. Most often, it seems, when making multiple requests in quick succession.
In an app I worked on recently we were hitting all sorts of strange behaviour. Sometimes
openParentApplication:reply: would mysteriously be called twice. Every once in a while the reply block would fail to ever be called — despite being certain the delegate was functioning correctly and completing work in a background task.
Frustrating, to say the least.
However, I’ve discovered a solution that has made communication with the iPhone rock-solid. Essentially you have to begin — and end, after two seconds — an empty background task right at the beginning of the delegate method. It should be the absolute first thing you do. Afterward, kick off a background task for the real work.
Here’s an example:
The theory is the bogus background task prevents the OS from killing your app immediately. I honestly couldn’t tell you for sure.
All I know is it’s made
openParentApplication:reply: far more reliable.