Skip to content

Commit

Permalink
Merge pull request #90 from luispadron/fix/trait-collection-main-queue
Browse files Browse the repository at this point in the history
Fix accessing trait collection outside main queue
  • Loading branch information
NickEntin authored Jun 16, 2022
2 parents 146b9f8 + 71d7170 commit 71d7288
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,23 @@ + (void)AS_setPreferredContentSizeCategoryOverride:(nullable UIContentSizeCatego

- (UITraitCollection *)AS_traitCollection;
{
UITraitCollection *traitCollection = [self AS_traitCollection];
__block UITraitCollection *traitCollection;

if (@available(iOS 15, *)) {
// TODO: On iOS 15+ simulators there is a main queue assertion crash.
// Investigation led us to find there is some UIKit internal code calling traitCollection
// on a background thread which then causes a UIKit main thread exception (since traitCollection needs to be accessed from main).
// We have not been able to find a solution for it besides this hack to force the call to happen on the main thread.
if ([NSThread isMainThread]) {
traitCollection = [self AS_traitCollection];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
traitCollection = [self AS_traitCollection];
});
}
} else {
traitCollection = [self AS_traitCollection];
}

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

0 comments on commit 71d7288

Please sign in to comment.