Caffeine++ 2.2 and WidgetKit
Recently I shipped a big update to my caffeine tracker, there’s a big list of new features. If you bought Caffeine++ 2, you get these for free. The main new feature of this update is widgets, but I also added pill tracking and an improved background refresh.
There are some changes I did under the hood which are not visible in the user interface.
- Refactoring completion blocks in Swift async/await, enjoyed this a lot
- Using the
@MainActorannotation and creating a dedicated
Actorfor data manipulation, should help eliminate any weird race conditions
In iOS 16 Apple introduced new ways to use
WidgetKit as Apple Watch complications. Turns out that getting it working is much harder than I thought. I spent a lot of time watching videos and trying sample code and finally got it working. However during testing, my code would fail during migration from existing complications into WidgetKit, the simulator would just show an empty space. It could be just a simulator issue, but I didn’t have patience to spend even more time on it. I implemented all necessary migration and used
CLKComplicationWidgetMigrator. I don’t see any immediate benefits because I still need to support watchOS 7 and
CLKComplicationDataSource which I invested a lot of time into. I will revisit this when watchOS 8 can be a minimum target.
WidgetKit was also challenging and cost me a lot of time. Initially, I couldn’t get it to work at all, my widgets would just display an empty state. My widget needs to query the Apple Health database and device file system. The biggest problem I had is that I tried to perform some work in SwiftUI and it just won’t execute. It only started to work when I moved my data processing into the
getTimtline functions inside
TimelineProvider. Due to the specifics of this app, I have to reload everything every time the data changes, it’s impossible to predict how much coffee you going to have in advance. If I would work on a calendar app where I know all the data in advance, it would be very simple, but in this case, it’s not. It seems like
WidgetKit suits some apps better than others. I still think it’s useful for people to see how much coffee they had without opening the app, and now with iOS 16, you can get this information from the Lock Screen. I use new widgets every day and they make Caffeine++ more useful.