Two kilometers into a run, the Apple Watch shows a route line over a blank grid. The iPhone that promised the map was available offline is back in the car.
The phone downloaded the route and finished its transfer. The runner needed one more fact before leaving the car. Could the Watch open the installed map with no signal?
Nothing on the phone was false. It had downloaded the pack. It had handed it to WatchConnectivity. Every fact it reported was true about the phone. “Available offline” is a fact about the Watch, and only the Watch has the authority to state it.
Download, transfer, and installation are three facts
Each pack has a region ID, a revision, and a manifest: the byte count and digest of what should arrive. The phone enqueues a downloaded pack with WCSession.transferFile(_:metadata:), carrying the region and revision in the metadata. On the Watch, the file delivered to session(_:didReceive:) lives in a location the system reclaims when the callback returns, so it is moved into app-owned staging storage before the method ends. Everything after that happens from staging.
The example below is original, for a single-file pack. It reads in bounded chunks, so a large pack never sits in the Watch’s memory at once, checks size and SHA-256 against the manifest, and cooperates with cancellation:
import CryptoKit
import Foundation
struct MapPackManifest: Sendable {
let regionID: String
let revision: UInt64
let byteCount: Int
let sha256: [UInt8]
}
enum MapPackError: Error {
case badManifest
case wrongSize
case wrongDigest
}
func verifyMapPack(_ url: URL, against manifest: MapPackManifest) throws {
guard manifest.byteCount > 0, manifest.sha256.count == 32 else {
throw MapPackError.badManifest
}
let handle = try FileHandle(forReadingFrom: url)
defer { try? handle.close() }
var bytesRead = 0
var hash = SHA256()
while true {
try Task.checkCancellation()
guard let chunk = try handle.read(upToCount: 64 * 1024),
!chunk.isEmpty else { break }
guard chunk.count <= manifest.byteCount - bytesRead else {
throw MapPackError.wrongSize
}
bytesRead += chunk.count
hash.update(data: chunk)
}
guard bytesRead == manifest.byteCount else { throw MapPackError.wrongSize }
guard Array(hash.finalize()) == manifest.sha256 else { throw MapPackError.wrongDigest }
}The digest proves the bytes match the manifest. It proves nothing about the manifest itself; trust in that comes from the app’s delivery protocol, a signed manifest or one fetched by the Watch over its own authenticated channel. And neither proves the bytes form a map. That takes the decoder. A pack is installed only after it verifies and the map engine opens it.
Promote atomically, keep the old one until then
The runner may update a region the night before a race. If the new revision fails, they still need the old one in the morning. So the installed pack stays in place while its replacement is verified, and promotion is a single replace:
struct InstalledPack: Sendable, Equatable {
let regionID: String
let revision: UInt64
}
func promote(
staged: URL,
manifest: MapPackManifest,
packsDirectory: URL,
opens: (URL) throws -> Void
) throws -> InstalledPack {
try verifyMapPack(staged, against: manifest)
try opens(staged) // the map engine reads it, or this throws
let destination = packsDirectory
.appending(path: "\(manifest.regionID).mappack")
if FileManager.default.fileExists(atPath: destination.path(percentEncoded: false)) {
_ = try FileManager.default.replaceItemAt(destination, withItemAt: staged)
} else {
try FileManager.default.moveItem(at: staged, to: destination)
}
return InstalledPack(regionID: manifest.regionID, revision: manifest.revision)
}An interrupted update leaves a known revision in place. A failed one leaves the previous revision and an error the phone can show. There is no moment at which the Watch holds half a map.
The receipt is specific, or it is noise
The phone should say “Available on Watch” only after the Watch says so, with a receipt that names the region and revision it installed. The phone compares it with the update it queued. Receipts can arrive late and out of order: a receipt for revision 5 may land while revision 6 is still waiting to transfer. A receipt that said only “map installed” would mark revision 6 ready on the strength of revision 5.
Give the Watch a storage budget and make it visible. A long route can cross several regions, and some will not fit. The phone should show which parts of the route have map coverage, let the runner remove packs, and never evict the pack an active workout is reading. The map and the route are separate evidence. A route line over a region with no tiles can still guide someone, as long as the interface says what it can and cannot show.
Prove the standalone workflow
Unit tests use a small fixture: it verifies; change one byte and it fails with wrongDigest; truncate the end and it fails with wrongSize; a newer revision arriving after the runner changed routes does not replace the active one until it passes.
Then the test that matters, on a paired Watch: receive the new map, turn the phone off, force-quit the Watch app, and open the map and route from a fresh launch. Repeat with an interrupted transfer, and with a route that runs past the edge of the downloaded region.
What to ask of your own code
- Which device makes the claim the interface shows, and is it the device the claim is about?
- Is the received file moved out of the system’s location before the delegate returns?
- Does installation require the decoder to open the file, not just the digest to match?
- Can a failed update ever leave the Watch with no usable map?
- Does each receipt name the exact revision it confirms?
A progress bar describes a transfer. The map that opens on the Watch, with the phone in the car, is the result. Only the second deserves the word “ready.”
References
- WCSession.transferFile(_:metadata:)
- WCSessionDelegate session(_:didReceive:)
- FileManager.replaceItemAt(_:withItemAt:backupItemName:options:)
Related editions
- Edition 20 · The Work Is Cancelled. The Cleanup Isn’t.
- Edition 36 · A Coordinate Is an Observation, Not the Truth
- Edition 42 · Restoring Focus After Playback on tvOS
If this edition sharpened your next review, you can buy me a coffee.