在另一个线程中运行 MBProgressHUD? [英] Run MBProgressHUD in another thread?

查看:28
本文介绍了在另一个线程中运行 MBProgressHUD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MBProgressHUD 来显示忙碌"动画以在填充 UITableView 时使用用户.UITableView 阻塞主线程,因此在表格加载完成之前动画甚至不会出现.

I am using MBProgressHUD to display a "busy" animation to use user while a UITableView is being populated. The UITableView blocks the main thread so the animation does not even appear until the table finishes loading.

有没有办法让忙动画在UITableView占用主线程的情况下运行在另一个线程上?

Is there a way to make the busy animation run on another thread while the UITableView occupies the main thread?

推荐答案

UIKit 在 run loop 完成当前循环时进行绘制.换句话说,如果您正在配置视图(例如 MBProgressHUD),则更改将在下一次运行循环迭代之前不可见.因此,如果您不允许通过阻塞主线程来使运行循环旋转,则 UI 更改不会立即出现.

UIKit does its drawing when the run loop completes the current cycle. In other words, if you're configuring a view (e.g., MBProgressHUD), the changes won't be visible until the next run loop iteration. Thus if you don't allow the run loop to spin by blocking the main thread, the UI changes won't appear immediately.

如果你不能在后台线程上完成你的工作,你需要在主线程上开始你的长时间运行的阻塞任务之前让运行循环完成它的循环.

If you can't do your work on a background thread, you need to allow the run loop to complete its cycle before you start your long-running blocking task on the main thread.

您可以通过在下一次运行循环迭代中安排执行来做到这一点.

You can do this by scheduling execution on the next run loop iteration.

// Setup and show HUD here

[self performSelector:@selector(myTask) withObject:nil afterDelay:0.001];

在 myTask 结束时隐藏 HUD.或者你可以手动运行 run loop.

Hide the HUD at the end of myTask. Or you can run the run loop manually.

// Setup and show HUD here

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];

// Insert myTask code here
// Hide the shown HUD here

或者(如果你可以使用块)

Or (if you can use blocks)

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.001 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
    // Insert myTask code here
});

这篇关于在另一个线程中运行 MBProgressHUD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆