Skip to content

fix: insert setupBackgroundObservers into multi-line didFinishLaunchingWithOptions - #372

Open
the-woody-kim wants to merge 1 commit into
kingstinct:masterfrom
the-woody-kim:fix/appdelegate-multiline-signature
Open

fix: insert setupBackgroundObservers into multi-line didFinishLaunchingWithOptions#372
the-woody-kim wants to merge 1 commit into
kingstinct:masterfrom
the-woody-kim:fix/appdelegate-multiline-signature

Conversation

@the-woody-kim

Copy link
Copy Markdown
Contributor

The problem

withAppDelegatePlugin inserts BackgroundDeliveryManager.shared.setupBackgroundObservers() by matching:

/(func application\(.+didFinishLaunchingWithOptions.+\{)\n/

. doesn't match newlines, so this only works when the whole signature is on one line. Expo SDK 54+ generates a multi-line AppDelegate:

public override func application(
  _ application: UIApplication,
  didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {

The match fails, String.replace returns the contents unchanged, and the setup call is never inserted.

It fails silently, which is what makes it expensive. String.replace doesn't throw on no-match, the import HealthKit insert immediately above it still succeeds, and the entitlement and Info.plist plugins still apply — so prebuild succeeds, the AppDelegate is visibly modified, and the entitlements are correct. Everything looks configured.

The runtime symptom is subtle too: subscribeToChanges still registers its own observer, so background delivery appears to work while the app is alive. It just never survives termination, since no observer is registered at launch. That's a long way from this regex.

The fix

Match with [^{]* instead of .+. It spans newlines, and refusing to cross a { also keeps the match from running out of an earlier application(...) overload into this one — the SDK 57 template has three func application( methods.

Also console.warn when the insert finds no match, so the next template change surfaces at prebuild instead of as missing background updates weeks later.

Verification

Against two fixtures, where the multi-line one deliberately places an application(_:open:options:) overload before didFinishLaunchingWithOptions to confirm the match can't span into it:

AppDelegate template before after
Expo SDK 54+ (multi-line) not inserted inserted, correct position
legacy (single-line) inserted, correct position inserted, correct position

So this fixes SDK 54+ without regressing the older single-line template.

Context

Follow-up to #365. @nopitown reported there that background updates stop arriving after a few deliveries on Expo — on a managed project this is at least one contributing cause, since without the launch-time registration only the observers created at runtime by subscribeToChanges exist, and those die with the process.

Biome passes on the changed file. Changeset included as a patch bump.

…ngWithOptions

The AppDelegate match used `.+`, which does not cross newlines. Expo SDK 54+
templates spread the didFinishLaunchingWithOptions signature over several
lines, so the match failed and String.replace returned the contents unchanged
-- silently, while the import HealthKit insert above it still succeeded.

Match with [^{]* instead: it spans newlines, and refusing to cross a brace
keeps it from running out of an earlier application(...) overload into this
one. Warn when the insert finds no match rather than failing silently.
@changeset-bot

changeset-bot Bot commented Jul 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d6d7de5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@kingstinct/react-native-healthkit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 27, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/kingstinct/react-native-healthkit/@kingstinct/react-native-healthkit@372

commit: d6d7de5

@nopitown

nopitown commented Jul 28, 2026

Copy link
Copy Markdown

Running this in Expo 54, I'm getting the following error when building my app:

../ios/Pods/Headers/Public/NitroModules/Null.hpp:8:10)

   6 |
   7 | #include "NitroDefines.hpp"
>  8 | #include <functional>
     |          ^ 'functional' file not found
   9 | #include <type_traits>
  10 |
  11 | namespace margelo::nitro {
  ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants