Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix accessing trait collection outside main queue #90

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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