Problem
I am developing an application that can track workout sessions for multiple paired Apple Watches (multiple watches paired to a single phone). I have implemented Watch Connectivity for multiple watches as per Apple Documentation, and that part of the application seems to be working okay. The application allows the user to initiate a workout session and pulls data from HealthKit so the user can view it in the future. I am having an issue with the application crashing (or at the very least, the handleActiveWorkoutRecovery
method is called) after starting a workout following a pairing switch of the watches. As a sample timeline to what occurs:
Time (minutes) | Event |
---|---|
0 | User starts workout session on Watch 1 |
5 | User stops workout session on Watch 1 |
5 | User switches pairing from Watch 1 to Watch 2 |
5 | User starts workout session on Watch 2 |
6 | The application "crashes" on Watch 2 |
I would include Crash logs, but the crash does not seem to show up anywhere. I have tried finding the logs through the organizer for TestFlight versions, and manually through sysdiagnose
and the Devices and Simulators
tab in XCode, but they just aren't anywhere, leaving me to hypothesize that this is not actually a crash (or at least that crash logs are not being generated).
What I have tried
I initially hypothesized there was some issue with the way I had implemented WCSession, but all three of the required methods session(_:activationDidCompleteWith:error:)
, sessionDidBecomeInactive(_:)
and sessionDidDeactivate(_:)
have been implemented in the code appropriately, as have all delegates. I also tried switching multiple times without starting a workout session to see if the application would crash, but it did not. I do not think this is the problem.
I also hypothesized that perhaps I was not ending the workout sessions appropriately and maybe the issue was something related to that. However, I am calling the appropriate .end()
and .stop()
methods for the workout session and HealthKit queries respectively. Additionally, I can restart the session on a single device after stopping it immediately, and it does not cause any issues. I do not think this is the problem either.
Current hypothesis
Currently, I hypothesize that the problem lies somewhere in starting the session too early after switching. So for example, while doing testing, I discovered that this timeline of events DOES NOT cause a crash
Time (minutes) | Event |
---|---|
0 | User starts workout session on Watch 1 |
5 | User stops workout session on Watch 1 |
5 | User switches pairing from Watch 1 to Watch 2 |
10 | User starts workout session on Watch 2 |
However, this timeline of events, DOES cause a crash.
Time (minutes) | Event |
---|---|
0 | User starts workout session on Watch 1 |
5 | User stops workout session on Watch 1 |
10 | User switches pairing from Watch 1 to Watch 2 |
10 | User starts workout session on Watch 2 |
11 | The application "crashes" on Watch 2 |
Notice in the previous test I had waited 5 minutes before initiating the session on the newly paired watch, and in this one, I waited 5 minutes before switching the pairing, but start the workout session immediately after the switch. To be absolutely clear, I always wait for the switch to complete (a tick mark appears in the watch app on the phone when the switch is complete) before starting a tracking session.
Question
To me, it seems like there is something going on after switching for at least a few minutes wherein starting a session will cause a crash. Unfortunately, I do not know what that is, so am wondering if there is a chain of events that occur after switching the watch pairing which will cause the workout session to crash if it starts too early. Right now the only workaround I can think of is not allowing the user to initiate a workout session for a specific period of time after a watch switch, but I want to know if there is some sort of event that I can tie that functionality to, instead of just blocking the user for a period of time.