• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 我会为 MBProgressHud 使用 block 中的调度吗?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 12:11:20 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在 MBProgressHud 文档中,它声明在调度中使用它,如下所示:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    // Do something...
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });
});

考虑到您不希望它搞砸主线程,这是完全可以理解的。但是我可以在使用 block 时这样做吗:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (error)
                {

                }
                else
                {
                   [MBProgressHUD hideHUDForView:self.view animated:YES];
                }
            }];

或者我还需要使用 dispatch 吗?



Best Answer-推荐答案


把你的代码改成这样:

 [MBProgressHUD showHUDAddedTo:self.view animated:YES];
 [object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
         NSLog(@"Am I running on the main thread: %@", [NSThread isMainThread] ? @"YES": @"NO");
         if (error)
         {

         }
         else
         {

         }
   }];

如果它记录“YES”那么你不需要在主线程上运行[MBProgressHUD hideHUDForView:self.view animated:YES];,否则你需要使用

dispatch_async(dispatch_get_main_queue(), ^{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
});

更新:

block 在调用它们的任何线程上运行,请注意以下示例:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self longRunningProcessWithCompletionBlock:^{
        NSLog(@"Is running on the main thread? %@", [NSThread isMainThread] ? @"YES" : @"NO");
    }];
}



- (void)longRunningProcessWithCompletionBlockvoid (^)(void))completionBlock {

    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    //this is running on the concurrent thread, so does completionBlock() as it has been called on a concurrent thread.
    dispatch_async(concurrentQueue, ^{
        [NSThread sleepForTimeInterval:3];
        completionBlock();
    });
}

所以基本上上面的结果将是 "Is running on the main thread? NO"

我再次对 viewDidLoad 进行了完全相同的调用:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self longRunningProcessWithCompletionBlock:^{
        NSLog(@"Is running on the main thread? %@", [NSThread isMainThread] ? @"YES" : @"NO");
    }];
}

但这一次,我在主线程上调用 longRunningProcessWithCompletionBlockcompletionBlock 如下:

- (void)longRunningProcessWithCompletionBlockvoid (^)(void))completionBlock {

    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(concurrentQueue, ^{

        [NSThread sleepForTimeInterval:3];

        //notice the difference, this time we are calling the completionBlock on the main thread, so that block will be running on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            completionBlock();
        });

    });
}

这一次因为我们在主线程上调用了完成 block ,结果会是是在主线程上运行吗?是的

简而言之,block 并不能保证它们会在主线程上执行!但它们可以保证它们将在任何调用它们的线程上执行。

在您的情况下,Parse.com 开发人员正在主线程上调用 deleteInBackgroundWithBlock 的完成处理程序 block ,这就是您看到日志为"is"的原因。所以您只需要调用 [MBProgressHUD hideHUDForView:self.view animated:YES]; 没有 dispatch_async(dispatch_get_main_queue(), ^{ }); (因为它已经在主线程,这是一个额外的不必要的步骤)

关于ios - 我会为 MBProgressHud 使用 block 中的调度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26557320/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap