【问题标题】:How to I correctly use async to correctly display HUD is this method?如何正确使用异步正确显示HUD是这种方法吗?
【发布时间】:2014-08-13 16:08:26
【问题描述】:

并发、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.");

      ...
   }
}

【问题讨论】:

    标签: ios objective-c concurrency grand-central-dispatch hud


    【解决方案1】:

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

    改成:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    

    【讨论】:

    • rmaddy,非常感谢您的意见。 HUD 区域的前两行现在是:/* BEGIN HUD AREA */ [MBProgressHUD showHUDAddedTo:appDel.rootTableViewController.view animated:YES]; //dispatch_async(dispatch_get_main_queue(), dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{ -- 问题仍然存在。 HUD 不会一直保持到长 DeepCopy 结束。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2010-11-21
    • 2021-01-18
    相关资源
    最近更新 更多