feat: register observer queries in AppDelegate for background delivery#341
Open
oakleaf wants to merge 1 commit intokingstinct:masterfrom
Open
feat: register observer queries in AppDelegate for background delivery#341oakleaf wants to merge 1 commit intokingstinct:masterfrom
oakleaf wants to merge 1 commit intokingstinct:masterfrom
Conversation
|
commit: |
…nd delivery Apple requires HKObserverQuery to be set up in didFinishLaunchingWithOptions before the JS bridge boots. Without this, background delivery silently fails when iOS terminates and relaunches the app for a HealthKit update. This adds BackgroundDeliveryManager — a plain Swift singleton (not NitroModules) that reads persisted type identifiers from UserDefaults at app launch and registers observer queries + enableBackgroundDelivery immediately. Events that arrive before JS is ready are queued and flushed when JS subscribes. New JS API: - configureBackgroundTypes(types, frequency) — persist + register observers - clearBackgroundTypes() — clear config + stop observers Expo config plugin now injects BackgroundDeliveryManager.shared.setupBackgroundObservers() into AppDelegate.didFinishLaunchingWithOptions automatically. Fixes kingstinct#51 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
302a12e to
3fb1b3f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #51 — HealthKit background delivery silently fails when the app is terminated because observer queries are only registered in JS-land, but Apple requires them in
didFinishLaunchingWithOptionsbefore the JS bridge boots.Changes
BackgroundDeliveryManager.swift(new) — Plain Swift singleton (not NitroModules) that:HKObserverQuery+enableBackgroundDeliveryfor each type immediatelynilpredicate (notDate.init()) to catch samples written while the app was deadCoreModule.swift— Two new JS-callable methods:configureBackgroundTypes(types, frequency)— persists config to UserDefaults + registers observers for current sessionclearBackgroundTypes()— clears persisted config + stops all observersapp.plugin.ts— Expo config plugin now injectsBackgroundDeliveryManager.shared.setupBackgroundObservers()intodidFinishLaunchingWithOptionsviawithAppDelegateTypeScript — New exports
configureBackgroundTypesandclearBackgroundTypesadded to Nitro spec, iOS implementation, non-iOS stubs, and test setupHow it works
configureBackgroundTypes(["HKQuantityTypeIdentifierStepCount", ...], UpdateFrequency.immediate)from JSBackgroundDeliveryManager.setupBackgroundObservers()runs from AppDelegate — before JS bridge — reads UserDefaults and registers observer queriesNon-Expo / bare RN
For apps not using Expo config plugins, add this to
AppDelegate.swift:Design decisions
didFinishLaunchingWithOptionstime before JS runs. UserDefaults is the simplest persistence that survives app termination.nilpredicate: The existingsubscribeToObserverQueryusesDate.init()which misses data written while the app was dead. Background observers need to catch everything.configureBackgroundTypesis additive — the existingenableBackgroundDelivery+subscribeToObserverQueryJS APIs continue to work unchanged for foreground use.Test plan
didFinishLaunchingWithOptionscontains the setup callconfigureBackgroundTypesfrom JS, verify UserDefaults populatedsubscribeToChangesstill works as beforeclearBackgroundTypesstops observers and clears UserDefaults🤖 Generated with Claude Code