Skip to content

Commit

Permalink
read App Windows in the main thread for view hierarchy fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
brustolin committed Jan 19, 2023
1 parent b2f82fa commit 917d84e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Sources/Sentry/SentryViewHierarchy.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ - (BOOL)saveViewHierarchy:(NSString *)filePath

- (NSData *)fetchViewHierarchy
{
NSArray<UIWindow *> *windows = [SentryDependencyContainer.sharedInstance.application windows];

__block NSMutableData *result = [[NSMutableData alloc] init];

void (^save)(void) = ^{
NSArray<UIWindow *> *windows = [SentryDependencyContainer.sharedInstance.application windows];

if (![self processViewHierarchy:windows
addFunction:writeJSONDataToMemory
userData:(__bridge void *)(result)]) {
Expand Down
23 changes: 23 additions & 0 deletions Tests/SentryTests/SentryViewHierarchyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,37 @@ class SentryViewHierarchyTests: XCTestCase {
wait(for: [ex], timeout: 1)
}

func test_fetch_usesMainThread() {
let sut = TestSentryViewHierarchy()
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
fixture.uiApplication.windows = [window]

let ex = expectation(description: "Running on background Thread")
let dispatch = DispatchQueue(label: "background")
dispatch.async {
let _ = sut.fetch()
ex.fulfill()
}

wait(for: [ex], timeout: 1)
XCTAssertTrue(fixture.uiApplication.calledOnMainThread, "fetchViewHierarchy is not using the main thread to get UI windows")
}

class TestSentryUIApplication: SentryUIApplication {
private var _windows: [UIWindow]?
private var _calledOnMainThread = true

var calledOnMainThread : Bool {
return _calledOnMainThread
}

override var windows: [UIWindow]? {
get {
_calledOnMainThread = Thread.isMainThread
return _windows
}
set {
_calledOnMainThread = Thread.isMainThread
_windows = newValue
}
}
Expand Down

0 comments on commit 917d84e

Please sign in to comment.