Skip to content

Commit bf04cdf

Browse files
friebetillTill Friebe
andauthored
fix(connectivity_plus): guard eventSink after engine teardown (iOS) (#3797)
Co-authored-by: Till Friebe <till@friebe.dev>
1 parent 4c623ae commit bf04cdf

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

packages/connectivity_plus/connectivity_plus/ios/connectivity_plus/Sources/connectivity_plus/ConnectivityPlusPlugin.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// be found in the LICENSE file.
44

55
import Flutter
6+
import UIKit
67

78
public class ConnectivityPlusPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
89
private let connectivityProvider: ConnectivityProvider
@@ -81,8 +82,15 @@ public class ConnectivityPlusPlugin: NSObject, FlutterPlugin, FlutterStreamHandl
8182
}
8283

8384
private func connectivityUpdateHandler(connectivityTypes: [ConnectivityType]) {
84-
DispatchQueue.main.async {
85-
self.eventSink?(self.statusFrom(connectivityTypes: connectivityTypes))
85+
DispatchQueue.main.async { [weak self] in
86+
guard let self = self, let eventSink = self.eventSink else { return }
87+
// NWPathMonitor can fire while the app is in the background, after the
88+
// FlutterEngine's shell has been torn down. Calling eventSink in that
89+
// state triggers an NSAssertion in -[FlutterEngine sendOnChannel:] and
90+
// crashes the app with SIGABRT. Skip the emission until the app is
91+
// foregrounded again; the next onListen / check call will resync state.
92+
guard UIApplication.shared.applicationState != .background else { return }
93+
eventSink(self.statusFrom(connectivityTypes: connectivityTypes))
8694
}
8795
}
8896

0 commit comments

Comments
 (0)