M-Swift All articles
AI & Performance

Watts in the Background: A Swift Team's Guide to Finding Battery Drain Before Users Do

M-Swift
Watts in the Background: A Swift Team's Guide to Finding Battery Drain Before Users Do

Photo: IPhone4SWhite_no_shadow.png: Zach.vega1 IPhone_3GS_YSoD.jpeg: havarhen derivative work: havarhen (talk), CC BY-SA 3.0, via Wikimedia Commons

Battery drain is weirdly personal. A user won't file a bug that says your BGAppRefreshTask ran nine times in an hour, but they will absolutely tell the App Store that their phone got hot while your app was just sitting there. Energy is one of the few performance problems people feel in their hand, which makes it a product problem long before it becomes a profiling problem.

The good news: you don't need a lab full of robots to find the worst offenders. You need a few real devices, a repeatable route through the app, and the discipline to look at energy the same way you look at launch time or crash-free sessions.

Start With the Stories Users Tell

Don't begin with Instruments. Begin with the complaints you already have. Common ones sound like: my phone warms up on the train, my battery drops overnight, the app chews through data, or my watch buzzes with stale updates. Each of those is a breadcrumb.

Pick two or three user journeys that map to those stories. A commute session with background audio, a morning check-in that triggers refresh and location, a quick browse that should not be doing much at all. Write down what the app is supposed to do during each journey, including what it should not do. That last part matters. Most energy bugs are not dramatic loops. They are small pieces of work that never got told to stop.

Build a Baseline on Real Hardware

The simulator is fine for logic. It is useless for energy. Use physical iPhones, including at least one older model if you still support it. Test on Wi-Fi and cellular, with Low Power Mode both on and off. Low Power Mode is not a magical test harness, but it changes how the system treats background work, and it can expose assumptions your code makes about timers and refresh opportunities.

Before each run, start from a known state: same starting battery percentage, same network conditions, same app data. Then run the journey for a fixed amount of time. Fifteen minutes is usually enough to see a pattern; an hour is better for background refresh and location. Repeat the run at least twice. Energy data is noisy, and one hot run can be a system update or a photo sync in the background.

Read the Energy Log Without Drowning

Instruments' Energy Log template gives you Energy Impact alongside CPU, GPU, networking, and display activity. Don't stare at the absolute number. Look for the shape of the graph and what it correlates with. A flat line that ticks up every 30 seconds is a timer. A burst after a network call is likely parsing or image decoding. Sustained elevated energy with low CPU often points at networking or location.

Compare the graph to your user journey timestamps. If you know the app was idle from minute four to minute ten, the graph should be boring there. If it isn't, you have a lead.

Put Signposts Around the Suspects

os_signpost is the cheapest way to connect code to the energy graph. Wrap the work you care about: background refresh begins and ends, location updates start and stop, a timer fires, a network request kicks off. You don't need to instrument everything. Instrument the things that could plausibly wake the radio or the CPU when the user isn't looking.

import os

let energyLog = OSLog(subsystem: "com.yourcompany.app", category: "Energy")

os_signpost(.begin, log: energyLog, name: "RefreshCycle")
// do refresh work
os_signpost(.end, log: energyLog, name: "RefreshCycle")

Now the Energy Log has context. You can see whether a spike lines up with a refresh cycle, a location callback, or nothing you recognize. That last case is usually the most expensive bug to find and the most valuable to fix.

Audit the Usual Thirsty Suspects

Background refresh is the classic offender. Check how often you request refresh, how long the work actually takes, and whether you bail out early when there is nothing new. A refresh task that always runs for 25 seconds because it waits on a slow API is a battery tax with no user benefit.

Timers deserve suspicion too. Repeating timers that fire while the app is in the background, Combine timers that never get cancelled, and polling loops disguised as "keep fresh" logic all add up. If a timer exists only to update a view that isn't visible, it shouldn't be running.

Location is another one. Look at the accuracy you request, the distance filter, and whether you pause updates automatically when the user stops moving. High-accuracy location running for a feature that only needs a city-level fix is a common and expensive mismatch.

Then there's network chatter. Analytics pings, config refreshes, image prefetches, and chatty polling can keep the radio awake far longer than the actual payload suggests. Coalesce requests where you can. Let the system batch background work. And question any poll that runs on a fixed interval just because it was easy to write.

Turn Findings Into a Budget

Once you have data, don't just file tickets. Give each major feature an energy expectation. Maybe the check-in flow should not add more than a small, agreed-upon amount of energy impact during a ten-minute idle run. Maybe background refresh should complete within a few seconds or cancel. The exact numbers matter less than the habit of having them.

Put the audit on the release checklist alongside launch time and crash-free rate. Run it before a big feature ships, after an SDK upgrade, and once a quarter for the app as a whole. Energy regressions are quiet. A recurring check is how you keep them from becoming reviews.

Battery life is not a mysterious force. It is the sum of small decisions about when your app wakes up, how long it stays awake, and whether it has a good reason. Measure those decisions on real devices, and you can fix the problem while it is still a graph instead of a complaint.

All Articles

Related Articles

Who's Actually Watching? Tracing SwiftUI Observation Dependencies After @Observable

Who's Actually Watching? Tracing SwiftUI Observation Dependencies After @Observable

The Dynamic Island Is Lying to You: A WidgetKit Refresh Audit That Finds the Real Lag

The Dynamic Island Is Lying to You: A WidgetKit Refresh Audit That Finds the Real Lag

Your App's First 400ms Are a Crime Scene: Triage Launch Time Before You Refactor