在另一个线程中运行 MBProgressHUD?

编程入门 行业动态 更新时间:2024-10-23 03:23:35
本文介绍了在另一个线程中运行 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?

本文发布于:2023-11-11 23:34:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   MBProgressHUD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!