Repository navigation
fix(connectivity_plus): emit the update missed in the background once the app is active (iOS) - #4008
Open
andrea689 wants to merge 1 commit into
Conversation
… the app is active (iOS) Since fluttercommunity#3797 the iOS plugin drops every connectivity update that arrives while UIApplication.applicationState is .background, to avoid sending on a FlutterEngine whose shell is gone. Nothing emits it later: a Dart listener that stays subscribed gets no new onListen, so after a network change made while the app was in the background (for example turning Wi-Fi off in Settings and coming back) onConnectivityChanged keeps reporting the old connectivity until the next path change. Remember that an update was dropped, and emit the current connectivity on UIApplication.didBecomeActiveNotification. The current path rather than the dropped one, since a newer update may still be queued. Updates that arrive while the app is active or inactive still go out at once and clear the flag, as do onCancel and detachFromEngine. Nothing is sent while the app is in the background, as before.
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.
Description
Since #3797 (7.3.0), the iOS plugin drops every connectivity update that arrives while
UIApplication.applicationStateis.background, so that it never sends on aFlutterEnginewhose shell is already gone. That PR expected the state to resync on the nextonListenorcheckcall. A listener that stays subscribed gets neither, soonConnectivityChangedkeeps reporting the old connectivity until the next path change, which may never come.Steps to reproduce on a physical iPhone, with an app that keeps a
Connectivity().onConnectivityChangedsubscription open:NWPathMonitorreports the change while the app is in the background, and the plugin drops it. The stream never emits[mobile], even after a minute in the foreground. With 6.1.5 it emitted[mobile]right away.This PR remembers that an update was dropped, and on
UIApplication.didBecomeActiveNotificationemits the current connectivity once:onCancelanddetachFromEngine.Why this does not bring back the crash #3797 fixed: the extra emission happens only on
didBecomeActive, when the app is.active. That is the same state in which every live update is already emitted today. The shell is destroyed on scene disconnect or terminate, and since #3957 the engine'sdealloccallsdetachFromEngine, which clears the sink and the flag. An engine kept alive after its shell is gone would already crash on the first path change in the foreground. This PR does not add a state in which the sink is called.To make this testable, the plugin's internal initializer now takes the application state and the notification center. They default to
UIApplication.shared.applicationStateandNotificationCenter.default. The example'sRunnerTests, until now the empty template, cover the event stream with a fakeConnectivityProvider, in 11 tests. With the initializer change alone, the 5 that cover the emission on becoming active fail. With the fix, all 11 pass.The CI workflow does not run the native tests. To run them:
Tested:
RunnerTestsabove, on an iPhone 17 simulator with iOS 27.0.flutter test integration_test/connectivity_plus_test.darton the same simulator.SWIFT_STRICT_CONCURRENCY=complete. It shows the same warnings inConnectivityPlusPlugin.swiftasmain, and none new.Related Issues
Checklist
CHANGELOG.mdnor the plugin version inpubspec.yamlfiles.flutter analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?
!in the title as explained in Conventional Commits).