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

查看:85
本文介绍了在另一个线程中运行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在运行循环完成当前循环时绘制图形。换句话说,如果您要配置视图(例如,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。
或者您可以手动运行运行循环。

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


$ b b

或者(如果可以使用块)

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天全站免登陆