Concurrency, GCD, HUD, iOS
Can some GCD expert tell me how to alter the following method, specifically the "HUD AREA" ? The HUD flashes for a few seconds when it needs to be up for about 45 seconds, while the "HUD AREA" code all finishes. I only need the corrected use of GCD (async) here. NSFetchedResultsControllers provide tableView control during DeepCopy run, where new data (unique) in the Default DB is moved into users existing DB. This code works, but the NSLog msgs continue to scroll away long after the HUD disappear. I am stuck. I am sorry I am so lame in this area.
Many Thanks for reading this, Mark
- (void)loadStore {
if (_store) {return;} // Don’t load store if it’s already loaded
iHungry_MeAppDelegate *appDel = (iHungry_MeAppDelegate*)[[UIApplication sharedApplication] delegate];
BOOL isMigrationNecessary = [self isMigrationNecessaryForStore:[appDel storeURL]];
if (isMigrationNecessary) { // DM Ver upgrade
[self performMigrationForStore:[appDel storeURL]]; // quick
}
BOOL newDataNeedsImporting =
[self isNewDefaultDataAlreadyImportedForStoreWithURL:appDel.storeURL
ofType:NSSQLiteStoreType]; // Data Ver upgrade // quick
if (newDataNeedsImporting) {
/* BEGIN HUD AREA */
[MBProgressHUD showHUDAddedTo:appDel.rootTableViewController.view animated:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[self loadSourceStore]; // quick
[self deepCopyFromPersistentStore:nil]; // LONG
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *options =
@{
NSMigratePersistentStoresAutomaticallyOption:@YES
,NSInferMappingModelAutomaticallyOption:@YES
};
NSError *error = nil;
DLog(@"Adding Main Store After DeepCopy");
_store = [_coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:[appDel storeURL]
options:options error:&error];
if (!_store) {NSLog(@"Failed to add store. Error: %@", error);
abort();
}
else{NSLog(@"Successfully added store: %@", _store);
}
[self setNewDefaultDataAsImportedForStore:_store];// in Store's MetaData
[MBProgressHUD hideHUDForView:appDel.rootTableViewController.view animated:YES];
});
});
/* END HUD AREA */
}else{
DLog(@"Normal Non-Upgrade Load.");
...
}
}
Your very first call to dispatch_async
is using the main queue instead of a background queue.
Change it to:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{