Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cocoa SDK init after hot-restart #2452

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
appRunner: () => runApp(MyApp()),
);
```
```
- Linux native error & obfuscation support ([#2431](https://github.com/getsentry/sentry-dart/pull/2431))
- Improve Device context on plain Dart and Flutter desktop apps ([#2441](https://github.com/getsentry/sentry-dart/pull/2441))
- Add debounce to capturing screenshots ([#2368](https://github.com/getsentry/sentry-dart/pull/2368))
Expand All @@ -53,6 +53,7 @@
### Fixes

- OS & device contexts missing on Windows ([#2439](https://github.com/getsentry/sentry-dart/pull/2439))
- Native iOS/macOS SDK session didn't start after Flutter hot-restart ([#2452](https://github.com/getsentry/sentry-dart/pull/2452))

### Dependencies

Expand Down
49 changes: 11 additions & 38 deletions flutter/ios/Classes/SentryFlutterPluginApple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin {

private static let nativeClientName = "sentry.cocoa.flutter"

// The Cocoa SDK is init. after the notification didBecomeActiveNotification is registered.
// We need to be able to receive this notification and start a session when the SDK is fully operational.
private var didReceiveDidBecomeActiveNotification = false

private var didBecomeActiveNotificationName: NSNotification.Name {
#if os(iOS)
return UIApplication.didBecomeActiveNotification
#elseif os(macOS)
return NSApplication.didBecomeActiveNotification
#endif
}

private static var pluginRegistrationTime: Int64 = 0

public static func register(with registrar: FlutterPluginRegistrar) {
Expand All @@ -40,7 +28,6 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin {
#endif

let instance = SentryFlutterPluginApple(channel: channel)
instance.registerObserver()
registrar.addMethodCallDelegate(instance, channel: channel)
}

Expand All @@ -51,22 +38,6 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin {

private lazy var sentryFlutter = SentryFlutter()

private func registerObserver() {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: didBecomeActiveNotificationName,
object: nil)
}

@objc private func applicationDidBecomeActive() {
didReceiveDidBecomeActiveNotification = true
// we only need to do that in the 1st time, so removing it
NotificationCenter.default.removeObserver(self,
name: didBecomeActiveNotificationName,
object: nil)

}

private lazy var iso8601Formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
Expand Down Expand Up @@ -352,15 +323,17 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin {
}
}

if didReceiveDidBecomeActiveNotification &&
(PrivateSentrySDKOnly.options.enableAutoSessionTracking ||
PrivateSentrySDKOnly.options.enableWatchdogTerminationTracking) {
// We send a SentryHybridSdkDidBecomeActive to the Sentry Cocoa SDK, so the SDK will mimics
// the didBecomeActiveNotification notification. This is needed for session and OOM tracking.
NotificationCenter.default.post(name: Notification.Name("SentryHybridSdkDidBecomeActive"), object: nil)
// we reset the flag for the sake of correctness
didReceiveDidBecomeActiveNotification = false
}
#if os(iOS) || targetEnvironment(macCatalyst)
let appIsActive = UIApplication.shared.applicationState == .active
#else
let appIsActive = NSApplication.shared.isActive
#endif

// We send a SentryHybridSdkDidBecomeActive to the Sentry Cocoa SDK, to mimic
// the didBecomeActiveNotification notification. This is needed for session, OOM tracking, replays, etc.
if appIsActive {
NotificationCenter.default.post(name: Notification.Name("SentryHybridSdkDidBecomeActive"), object: nil)
}

#if canImport(UIKit) && !SENTRY_NO_UIKIT
#if os(iOS) || os(tvOS)
Expand Down
Loading