So I am a beginner to iOS development and especially developing iOS widgets.
I wrote this widget for my app which is mostly working how I expect. The app is a calorie tracking app, and the widget shows how many calories you’ve burned vs how many you’ve consumed today, then does the subtraction to show your caloric deficit.
The problem that I’m having is, as day goes on, and the widget keeps refreshing to update itself, it occasional fails entirely to pull the caloric burn information from HealthKit. This leaves the widget in a bizarre broken state, which resolves itself if ignored for long enough.
(don’t worry that it’s 100 consumed calories instead of 500 like the previous screenshot, that’s not a problem, I just changed it between screenshots. The consumed calories portion of this works properly. It’s only the HealthKit part that zeroes itself out unexpectedly)
I have no clue why this happening and I don’t know how I might update the implementation to be more reliable. Part of this is that I don’t fully understand swift and how to manage the callback hell that comes with nesting HealthKit’s completion handlers.
It takes a while before this happens, and you have to catch it at the right time otherwise it fixes itself the next time the widget refreshes. Therefore I haven’t been able to attach a debugger while it was happening to figure out what happened.
For reference:
- this is my widget source code: https://github.com/johnpc/jpc-fit/blob/main/ios/App/JpcFitWidget/JpcFitWidget.swift#L63
- this is my published app, which includes this widget: https://apps.apple.com/us/app/jpc-fit/id6482482386
Any advice would be helpful – do not underestimate how little I know about developing for the iOS ecosystem. Code feedback, debugging / logging tools, anything can be helpful in tracking down why this happens. I appreciate your advice.