DEV Community

Dmitry Matyukhin
Dmitry Matyukhin

Posted on AI-assisted

BGAppRefreshTask barely ran for my users, so the widget does the work now

I'm building an iOS app that monitors Kubernetes clusters and sends an alert when something breaks. There's no backend. The phone connects to the clusters directly with the user's own credentials, so I have nothing that could send a push, and every check has to run on the device.

I assumed background refresh would cover that. It didn't, and most of this post is about what I ended up using instead.

Background refresh

BGAppRefreshTask lets you ask for time and iOS decides if and when you get it. For an app people only open now and then, that can mean never. Apple DTS says there are "common scenarios where it won't grant you any". Force-quit the app or turn on Low Power Mode and it doesn't run at all.

My TestFlight users were seeing widgets that said "Checked 1 day ago". Nothing was broken, the task just wasn't being scheduled. I still register it, but I stopped relying on it.

Streaming connections

The app keeps lists up to date with long-lived watch connections. Those only live while the app is in the foreground: iOS suspends the sockets soon after you leave, and background URLSession can't stream.

So I treat backgrounding as an ordinary disconnect. When the app comes back it does a fresh list and starts watching again. While it's open, a check runs every 30 seconds, started and stopped with scenePhase.

None of that helps when the app is closed, which is most of the time.

Widgets

The thing that did run regularly was the widget. A widget people actually look at gets around 40-70 timeline reloads a day. A device log I pulled showed one every 15-60 minutes. And a widget extension can make its own network requests, mTLS with a keychain identity included.

So now the widget runs the check. On reload, if the stored result is older than five minutes, it runs the same health check as the app and sends the notifications itself. There's also a Refresh button on the widget, an App Intent that runs inside the extension.

Limits I put on the extension

Extensions get much less memory and time than the app, so I kept the check small:

  • plain list requests, no watches
  • 8 s timeout per request, 12 s for the whole check
  • only one check at a time
  • if two checks in a row never finish, stop checking for six hours

If a check can't reach anything, it doesn't overwrite the last result. Otherwise a phone off the VPN would show every cluster as down. It shows the last result and how old it is instead.

Sharing state between the app and the widget

Now there are two processes that can notice the same problem and both send an alert. They share everything through the App Group (the container an app and its extensions can both read and write):

  • alert rules and the last snapshot live in the App Group's UserDefaults suite
  • every sent alert is written to an inbox file in the shared container
  • the 5-minute cooldown is checked against that inbox, not against memory, so only one process sends

Credentials go in a keychain item with the App Group's access group, so the extension builds its connection with the same code as the app.

Two bugs I only saw on a real device

The extension has its own Info.plist. The app has an ATS exception because it does its own certificate checks for clusters. I hadn't copied it to the widget extension, so iOS rejected the connection with -9802 before my code ran, and every widget check sat there until the timeout. My local test cluster was on 127.0.0.1, which is why I didn't catch it earlier.

Widgets reload while the phone is locked. I'd stored the client identity with kSecAttrAccessibleWhenUnlocked, so on a locked phone every check failed with -25308. Switching to kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly fixed it. If you change this, look for old items saved with the previous setting: one unreadable item can make the whole identity query fail with the same error.

What it still can't do

It's not a pager. When the app is closed, alerts depend on the user having the widget installed and on how often WidgetKit reloads it. The settings screen says that. Real-time alerts would need a server forwarding to APNs. I've sketched that design but haven't built it.

If you're doing periodic work on iOS without a server, how are you handling it? I'd like to know if anyone found something more reliable than widget reloads.

Top comments (0)