Skip to content

Commit e8c8a05

Browse files
fix(sdk): Collect only unique UIWindow references (#4159)
When scene and app delegate holds reference to the same windows, it's added twice to the array.
1 parent bb71736 commit e8c8a05

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Fixes
1010

1111
- Session replay crash when writing the replay (#4186)
12+
- Collect only unique UIWindow references (#4159)
1213

1314
### Deprecated
1415

Sources/Sentry/SentryUIApplication.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ - (UIApplication *)sharedApplication
6969
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
7070
dispatchSyncOnMainQueue:^{
7171
UIApplication *app = [self sharedApplication];
72-
NSMutableArray *result = [NSMutableArray array];
72+
NSMutableSet *result = [NSMutableSet set];
7373

7474
if (@available(iOS 13.0, tvOS 13.0, *)) {
7575
NSArray<UIScene *> *scenes = [self getApplicationConnectedScenes:app];
@@ -91,7 +91,7 @@ - (UIApplication *)sharedApplication
9191
[result addObject:appDelegate.window];
9292
}
9393

94-
windows = result;
94+
windows = [result allObjects];
9595
}
9696
timeout:0.01];
9797
return windows ?: @[];

Tests/SentryTests/SentryUIApplicationTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,43 @@ class SentryUIApplicationTests: XCTestCase {
3838
XCTAssertEqual(sut.windows?.count, 1)
3939
}
4040

41+
//Somehow this is running under iOS 12 and is breaking the test. Disabling it.
42+
@available(iOS 13.0, tvOS 13.0, *)
43+
func test_applicationWithScenesAndDelegateWithWindow_Unique() {
44+
let sceneDelegate = TestUISceneDelegate()
45+
sceneDelegate.window = UIWindow()
46+
let scene1 = MockUIScene()
47+
scene1.delegate = sceneDelegate
48+
49+
let delegate = TestApplicationDelegate()
50+
delegate.window = UIWindow()
51+
52+
let sut = MockSentryUIApplicationTests()
53+
sut.scenes = [scene1]
54+
sut.appDelegate = delegate
55+
56+
XCTAssertEqual(sut.windows?.count, 2)
57+
}
58+
59+
//Somehow this is running under iOS 12 and is breaking the test. Disabling it.
60+
@available(iOS 13.0, tvOS 13.0, *)
61+
func test_applicationWithScenesAndDelegateWithWindow_Same() {
62+
let window = UIWindow()
63+
let sceneDelegate = TestUISceneDelegate()
64+
sceneDelegate.window = window
65+
let scene1 = MockUIScene()
66+
scene1.delegate = sceneDelegate
67+
68+
let delegate = TestApplicationDelegate()
69+
delegate.window = window
70+
71+
let sut = MockSentryUIApplicationTests()
72+
sut.scenes = [scene1]
73+
sut.appDelegate = delegate
74+
75+
XCTAssertEqual(sut.windows?.count, 1)
76+
}
77+
4178
//Somehow this is running under iOS 12 and is breaking the test. Disabling it.
4279
@available(iOS 13.0, tvOS 13.0, *)
4380
func test_applicationWithScenes_noWindow() {

0 commit comments

Comments
 (0)