Building an offline photo cleaner with Vision and Apple Foundation Models on iPhone A developer on the ClearWhirl team built an on-device iPhone photo cleaner that runs all pixel analysis through Apple's Vision framework on downscaled copies, using a capped dispatch queue and a local index, while Apple's Foundation Models handle only the task of converting natural-language queries into typed search filters. The app keeps all processing on the phone and works in airplane mode, and the developer documented fixes for two production issues: a scan freeze caused by blocking work inside Task.detached, resolved with a semaphore-limited dispatch queue, and premature background-task submission that suspended scans, resolved by submitting BGContinuedProcessingTaskRequest before the file-size phase. Disclosure: I work on ClearWhirl, the iPhone photo cleaner this post is about. It is on the App Store: https://apps.apple.com/us/app/phone-cleaner-clearwhirl-ai/id6835310950 https://apps.apple.com/us/app/phone-cleaner-clearwhirl-ai/id6835310950 Short version: everything that looks at pixels runs through Apple's Vision framework on downscaled copies, on a capped dispatch queue, with results saved to a local index. Apple's on-device Foundation Models never see a photo. They do one job: turn "payment receipts from 2025" into a typed search filter. When the model is unavailable, search falls back to plain rules. Nothing leaves the phone, and the app works in airplane mode. Stage A reads only PHAsset metadata and fills several folders in seconds: if a.mediaSubtypes.contains .photoScreenshot { f .screenshots, default: .append id ; continue } if a.mediaType == .video { if i.bytes 100 000 000 || a.duration 120 { f .largeVideos, default: .append id } continue } if a.representsBurst || a.mediaSubtypes.contains .photoLive && i.bytes 6 000 000 { f .bursts, default: .append id } Favourites go to a "Keepers" folder and are protected from every other folder by default. Stage B reads pixels, once per photo: VNGenerateImageFeaturePrintRequest , VNClassifyImageRequest and VNDetectBarcodesRequest QR, accepted at confidence ≥ 0.9 with a payload , VNRecognizeTextRequest when the labels suggest a receipt, ticket, document or similar, and for every screenshot. Small print is unreadable at thumbnail size. Comparing every photo with every other photo is quadratic. Most near-duplicates are taken seconds apart, so the scan walks photos in capture order and only compares each one with the previous one: js // Similar: compare with the previous photo if taken within 3 minutes. if let print, let pp = prevPrint, let p = prev, info.date.timeIntervalSince p.date < 180 { var d: Float = 99 try? print.computeDistance &d, to: pp if d < 0.42 { if currentGroup.isEmpty { currentGroup = p.id } currentGroup.append info.id } else { if currentGroup.count 1 { groups.append currentGroup } currentGroup = } } The trade-off is explicit: two similar photos taken a day apart are not grouped. The best shot in a group is the highest aesthetics score where available, with sharpness as tie-break, and only the extras count towards "space you could free". Our first version ran image loads and Vision requests inside Task.detached . The scan froze after the first batch. Synchronous, blocking work starves the cooperative thread pool. The fix was a plain dispatch queue with a semaphore, bridged with a continuation: js nonisolated static let workQueue = DispatchQueue label: "cw.analysis", qos: .utility, attributes: .concurrent nonisolated static let workLimit = DispatchSemaphore value: 4 nonisolated static func onWorkQueue