From 79b82d698e4a6f60d74293215dc506602b2b1f1c Mon Sep 17 00:00:00 2001 From: Robert Tolar Haining Date: Thu, 24 Aug 2017 11:08:44 -0400 Subject: [PATCH] Fixes some issues around Xcode 9 yelling about using UIKit on background threads --- .../FastImageCache/FICImageTable.m | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/FastImageCache/FastImageCache/FastImageCache/FICImageTable.m b/FastImageCache/FastImageCache/FastImageCache/FICImageTable.m index 2925c02..416d2bb 100644 --- a/FastImageCache/FastImageCache/FastImageCache/FICImageTable.m +++ b/FastImageCache/FastImageCache/FastImageCache/FICImageTable.m @@ -31,6 +31,8 @@ static NSString *const FICImageTableMRUArrayKey = @"mruArray"; static NSString *const FICImageTableFormatKey = @"format"; +static BOOL FICProtectedDataAvailable = NO; + #pragma mark - Class Extension @interface FICImageTable () { @@ -134,6 +136,29 @@ + (NSString *)directoryPath { return __directoryPath; } ++ (void)initialize { + [self registerForProtectedDataNotifications]; +} + ++ (void)registerForProtectedDataNotifications { + dispatch_async(dispatch_get_main_queue(), ^{ + UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)]; + if (application) { + FICProtectedDataAvailable = [application isProtectedDataAvailable]; + } + }); + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(protectedDataWillBecomeUnavailable:) name:UIApplicationProtectedDataWillBecomeUnavailable object:nil]; + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(protectedDataDidBecomeAvailable:) name:UIApplicationProtectedDataDidBecomeAvailable object:nil]; +} + ++ (void)protectedDataWillBecomeUnavailable:(id)sender { + FICProtectedDataAvailable = NO; +} ++ (void)protectedDataDidBecomeAvailable:(id)sender { + FICProtectedDataAvailable = YES; +} + + #pragma mark - Object Lifecycle - (instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImageCache *)imageCache { @@ -227,7 +252,7 @@ - (instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImag [self.imageCache _logMessage:message]; self = nil; - } + } } return self; @@ -518,10 +543,8 @@ - (BOOL)canAccessEntryData { // -[UIApplication isProtectedDataAvailable] checks whether the keybag is locked or not UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)]; - if (application) { - _canAccessData = [application isProtectedDataAvailable]; - } - + _canAccessData = FICProtectedDataAvailable; + // We have to fallback to a direct check on the file if either: // - The application doesn't exist (happens in some extensions) // - The keybag is locked, but the file might still be accessible because the mode is "until first user authentication"