Skip to content

Commit 71d7288

Browse files
authored
Merge pull request #90 from luispadron/fix/trait-collection-main-queue
Fix accessing trait collection outside main queue
2 parents 146b9f8 + 71d7170 commit 71d7288

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Sources/AccessibilitySnapshot/Core/ObjC/UIView+DynamicTypeSnapshotting.m

+17-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,23 @@ + (void)AS_setPreferredContentSizeCategoryOverride:(nullable UIContentSizeCatego
5555

5656
- (UITraitCollection *)AS_traitCollection;
5757
{
58-
UITraitCollection *traitCollection = [self AS_traitCollection];
58+
__block UITraitCollection *traitCollection;
59+
60+
if (@available(iOS 15, *)) {
61+
// TODO: On iOS 15+ simulators there is a main queue assertion crash.
62+
// Investigation led us to find there is some UIKit internal code calling traitCollection
63+
// on a background thread which then causes a UIKit main thread exception (since traitCollection needs to be accessed from main).
64+
// We have not been able to find a solution for it besides this hack to force the call to happen on the main thread.
65+
if ([NSThread isMainThread]) {
66+
traitCollection = [self AS_traitCollection];
67+
} else {
68+
dispatch_sync(dispatch_get_main_queue(), ^{
69+
traitCollection = [self AS_traitCollection];
70+
});
71+
}
72+
} else {
73+
traitCollection = [self AS_traitCollection];
74+
}
5975

6076
if (contentSizeCategoryOverride != nil) {
6177
UITraitCollection *contentSizeCategoryTraitCollection = [UITraitCollection traitCollectionWithPreferredContentSizeCategory:contentSizeCategoryOverride];

0 commit comments

Comments
 (0)