Skip to content

Commit

Permalink
Merge 32f9707 into cb2eefe
Browse files Browse the repository at this point in the history
  • Loading branch information
brustolin authored Jan 19, 2023
2 parents cb2eefe + 32f9707 commit d67ed1b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- AttachScreenshots is GA (#2623)
- Gather profiling timeseries metrics for CPU usage and memory footprint, and thermal and memory pressure events (#2493)

### Fixes

- Reads UI Windows in the main thread while fetching view hierarchy (#2629)

## 8.0.0

### Features
Expand Down
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 d67ed1b

Please sign in to comment.