From a8d177087111c3782209caa45ef54e6f948e3568 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 15 Sep 2017 00:51:37 -0700 Subject: [PATCH] Fix Main Thread Checker safety issue in DataManager --- Limelight/Database/DataManager.m | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Limelight/Database/DataManager.m b/Limelight/Database/DataManager.m index 1f1aee3..afc6141 100644 --- a/Limelight/Database/DataManager.m +++ b/Limelight/Database/DataManager.m @@ -19,7 +19,17 @@ - (id) init { self = [super init]; - _appDelegate = [[UIApplication sharedApplication] delegate]; + // HACK: Avoid calling [UIApplication delegate] off the UI thread to keep + // Main Thread Checker happy. + if ([NSThread isMainThread]) { + _appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; + } + else { + dispatch_sync(dispatch_get_main_queue(), ^{ + _appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; + }); + } + _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; [_managedObjectContext setPersistentStoreCoordinator:_appDelegate.persistentStoreCoordinator];