Skip to content

Commit 9faf217

Browse files
fix: Reads UI Windows in the main thread while fetching view hierarchy (#2629)
Reads UI Windows in the main thread while fetching view hierarchy Co-authored-by: Philipp Hofmann <[email protected]>
1 parent 5cabf7e commit 9faf217

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
- Support uint64 in crash reports (#2631)
1313

14+
### Fixes
15+
16+
- Always fetch view hierarchy on the main thread (#2629)
17+
1418
## 8.0.0
1519

1620
### Features

Sources/Sentry/SentryViewHierarchy.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ - (BOOL)saveViewHierarchy:(NSString *)filePath
4848

4949
- (NSData *)fetchViewHierarchy
5050
{
51-
NSArray<UIWindow *> *windows = [SentryDependencyContainer.sharedInstance.application windows];
52-
5351
__block NSMutableData *result = [[NSMutableData alloc] init];
5452

5553
void (^save)(void) = ^{
54+
NSArray<UIWindow *> *windows =
55+
[SentryDependencyContainer.sharedInstance.application windows];
56+
5657
if (![self processViewHierarchy:windows
5758
addFunction:writeJSONDataToMemory
5859
userData:(__bridge void *)(result)]) {

Tests/SentryTests/SentryViewHierarchyTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,37 @@ class SentryViewHierarchyTests: XCTestCase {
172172
wait(for: [ex], timeout: 1)
173173
}
174174

175+
func test_fetch_usesMainThread() {
176+
let sut = TestSentryViewHierarchy()
177+
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
178+
fixture.uiApplication.windows = [window]
179+
180+
let ex = expectation(description: "Running on background Thread")
181+
let dispatch = DispatchQueue(label: "background")
182+
dispatch.async {
183+
let _ = sut.fetch()
184+
ex.fulfill()
185+
}
186+
187+
wait(for: [ex], timeout: 1)
188+
XCTAssertTrue(fixture.uiApplication.calledOnMainThread, "fetchViewHierarchy is not using the main thread to get UI windows")
189+
}
190+
175191
class TestSentryUIApplication: SentryUIApplication {
176192
private var _windows: [UIWindow]?
193+
private var _calledOnMainThread = true
194+
195+
var calledOnMainThread: Bool {
196+
return _calledOnMainThread
197+
}
177198

178199
override var windows: [UIWindow]? {
179200
get {
201+
_calledOnMainThread = Thread.isMainThread
180202
return _windows
181203
}
182204
set {
205+
_calledOnMainThread = Thread.isMainThread
183206
_windows = newValue
184207
}
185208
}

0 commit comments

Comments
 (0)