Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

cocoa touch - Run MBProgressHUD in another thread?

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

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
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...