Dispatch main screen scale to main once and only once

fixes #183
This commit is contained in:
Ryan Nystrom 2015-01-08 14:24:34 -08:00
parent 42f801f269
commit e60b51bbbf

View File

@ -51,18 +51,25 @@ CGFloat ASDisplayNodeScreenScale()
{
static CGFloat screenScale = 0.0;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if ([NSThread isMainThread]) {
ASDispatchOnceOnMainThread(&onceToken, ^{
screenScale = [[UIScreen mainScreen] scale];
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
screenScale = [[UIScreen mainScreen] scale];
});
}
});
return screenScale;
}
void ASDispatchOnceOnMainThread(dispatch_once_t *predicate, dispatch_block_t block)
{
if ([NSThread isMainThread]) {
dispatch_once(predicate, block);
} else {
if (DISPATCH_EXPECT(*predicate == 0L, NO)) {
dispatch_sync(dispatch_get_main_queue(), ^{
dispatch_once(predicate, block);
});
}
}
}
void ASDisplayNodePerformBlockOnMainThread(void (^block)())
{
if ([NSThread isMainThread]) {