UIAlertView starts to show, screen dims, but it doesn't pop up until it's too late!

对着背影说爱祢 提交于 2019-12-31 03:30:10

问题


I have a button which when pressed makes calls that load information from a series of URLs (about 5 seconds loading time). Right before actually making those calls I want to add a "loading" alert. When I use a UIAlertView the screen dims like it's about to pop up, but it doesn't until the data is loaded - too late! I have no idea what's going on, it's like the calls I'm making to load the data are immediately taking preference over showing the new view, even though they're made right after the calls adding the new view (or showing the alert). This is a summarized version of the code:

-(void) refresh{
   UIAlertView *av = ...
   [av show]; //this should pop up before dat begins to load
   [myDataSource loadData]; //this contains a series of [NSData initWithURL] calls
   [self.tableView reloadData];
   //here I would hide the AlertView, but if I do I see it for just s split second
   //when the tableView has already reloaded
} 

Thanks in advance for any insight!

***EDIT To anyone who uses performSelectorInBackground beware of the added complexities of creating what is effectively a threaded program. For example, leaks might appear as the new thread doesn't have an autorelease pool - you have to add one, etc.


回答1:


when you try to retrieve data from the internet (I guess you are using something like [NSString stringWithContentsOfURL: ...]) the main thread is waiting for those data and for this reason the application cannot redraw the interface. You can try to use:

[mySourceData performSelectorInBackground:@selector(loadData) withObject:nil];

Be careful if you are using coredata to lock the NSManagedObjectContext before doing any operation on it and unlock it when you finish.




回答2:


If you have a bunch of operations to perform, in addition to performSelectorInBackground: like cescofry wrote, you can use NSOperation and NSOperationQueue.




回答3:


You probably don't want an UIAlertView for this anyway. Take a look at MBProgressHUD on GitHub.



来源:https://stackoverflow.com/questions/5185223/uialertview-starts-to-show-screen-dims-but-it-doesnt-pop-up-until-its-too-la

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!