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: Reading applicationState in the background #3372

Merged
merged 7 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Stop sending empty thread names (#3361)
- Work around edge case with a thread info kernel call sometimes returning invalid data, leading to a crash (#3364)
- Reading applicationState in the background (#3372)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: I don't this requires a changelog entry as it fixes a fix 😄

brustolin marked this conversation as resolved.
Show resolved Hide resolved

## 8.14.2

Expand Down
44 changes: 42 additions & 2 deletions Sources/Sentry/SentryUIApplication.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
#import "SentryUIApplication.h"
#import "SentryDependencyContainer.h"
#import "SentryNSNotificationCenterWrapper.h"

#if SENTRY_HAS_UIKIT

# import <UIKit/UIKit.h>

@implementation SentryUIApplication
@implementation SentryUIApplication {
UIApplicationState appState;
}

- (instancetype)init
{
if (self = [super init]) {

[SentryDependencyContainer.sharedInstance.notificationCenterWrapper
addObserver:self
selector:@selector(didEnterBackground)
name:UIApplicationDidEnterBackgroundNotification];

[SentryDependencyContainer.sharedInstance.notificationCenterWrapper
addObserver:self
selector:@selector(didBecomeActive)
name:UIApplicationDidBecomeActiveNotification];
// We store the application state when the app is initialized
// and we keep track of its changes by the notifications
// this way we avoid calling sharedApplication in a background thread
appState = self.sharedApplication.applicationState;
}
return self;
}

- (void)dealloc
{
[SentryDependencyContainer.sharedInstance.notificationCenterWrapper removeObserver:self];
}

- (UIApplication *)sharedApplication
{
Expand Down Expand Up @@ -58,7 +88,17 @@ - (UIApplication *)sharedApplication

- (UIApplicationState)applicationState
{
return self.sharedApplication.applicationState;
return appState;
}

- (void)didEnterBackground
{
appState = UIApplicationStateBackground;
}

- (void)didBecomeActive
{
appState = UIApplicationStateActive;
}

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import SentryTestUtils

@objcMembers public class TestNSNotificationCenterWrapper: SentryNSNotificationCenterWrapper {

var ignoreRemoveObserver = false

var addObserverInvocations = Invocations<(observer: Any, selector: Selector, name: NSNotification.Name)>()
public var addObserverInvocationsCount: Int {
return addObserverInvocations.count
Expand All @@ -24,6 +27,8 @@
return removeObserverInvocations.count
}
public override func removeObserver(_ observer: Any) {
removeObserverInvocations.record(observer)
if ignoreRemoveObserver == false {
removeObserverInvocations.record(observer)

Check warning on line 31 in Tests/SentryTests/Helper/TestNSNotificationCenterWrapper.swift

View check run for this annotation

Codecov / codecov/patch

Tests/SentryTests/Helper/TestNSNotificationCenterWrapper.swift#L31

Added line #L31 was not covered by tests
}
}
}
26 changes: 26 additions & 0 deletions Tests/SentryTests/SentryUIApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ class SentryUIApplicationTests: XCTestCase {

XCTAssertEqual(sut.windows?.count, 0)
}

@available(iOS 13.0, tvOS 13.0, *)
func test_ApplicationState() {
let notificationCenter = TestNSNotificationCenterWrapper()
notificationCenter.ignoreRemoveObserver = true
SentryDependencyContainer.sharedInstance().notificationCenterWrapper = notificationCenter

let sut = MockSentryUIApplicationTests()
XCTAssertEqual(sut.applicationState, .active)

notificationCenter.addObserverInvocations.invocations.forEach { (observer: Any, selector: Selector, name: NSNotification.Name) in
if name == UIApplication.didEnterBackgroundNotification {
sut.perform(selector, with: observer)
}
}

XCTAssertEqual(sut.applicationState, .background)

notificationCenter.addObserverInvocations.invocations.forEach { (observer: Any, selector: Selector, name: NSNotification.Name) in
if name == UIApplication.didBecomeActiveNotification {
sut.perform(selector, with: observer)
}
}

XCTAssertEqual(sut.applicationState, .active)
}

private class TestApplicationDelegate: NSObject, UIApplicationDelegate {
var window: UIWindow?
Expand Down