开发者

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

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{
   UIAlertVie开发者_JAVA百科w *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.


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜