0

并发、GCD、HUD、iOS

一些 GCD 专家可以告诉我如何更改以下方法,特别是“HUD AREA”吗?当 HUD 需要启动大约 45 秒时,它会闪烁几秒钟,而“HUD AREA”代码全部完成。我只需要在这里正确使用 GCD(异步)。NSFetchedResultsControllers 在 DeepCopy 运行期间提供 tableView 控制,其中默认数据库中的新数据(唯一)被移动到用户现有数据库中。此代码有效,但 NSLog 消息在 HUD 消失后很长时间继续滚动。我被困住了。对不起,我在这方面很蹩脚。

非常感谢您阅读本文,马克

- (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.");

      ...
   }
}
4

1 回答 1

3

您的第一个调用dispatch_async是使用主队列而不是后台队列。

将其更改为:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
于 2014-08-13T17:08:12.057 回答