简体   繁体   中英

UIAlertView Loading indicator doesn't work in a secondary thread

I have following code for the UIAlertView Loading indicator which is not working and giving me

- (void) launchActivity
{
 //some logic...
[NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil];
}
- (void) updateFilterProgress {
 if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
    UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"No Internet Connectivity" message:@"This app require an internet connection via WiFi or cellular network to work." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
    [myAlert show];
}
else{
    UIAlertView *alertMe = [[[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil] autorelease] ;


  //tried this way by placing below line....no result   
  [alertMe performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
    //[alertMe show];

 //some logic...
}

Updated: On main thread I am calling web-service and loading data. hence I have given another thread for Loading UIAlertView and it was working with iOS 4,5. but its crashing in iOS 6. If I am placing AlerView on main thread then while loading nothing shows but after getting data loaded AlertView shows Loading Indicator for few seconds. Any suggestion...

You are showing the alert from the detached thread while you must do it from the main thread, either use GCD or performSelectorOnMainThread .


On the main thread you usually want to perform only the UI update, all the complex calculations and data loading are to be executed in the detached threads. If you try to load the data in the main thread, the UI will no be responding during the loading. So that is a good practice to load the data in the detached thread, on the main thread you show the alert as you need on the loading start and dismiss it on the loading (and parsing) finished, calling the content UI update as well:

加载/刷新数据源流

It's a bad practise.

Apple docs say that you need to handle the UI elements on the main thread.

I think the issue is with this line:

[NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil];

You won't handle the UI element on other threads rather than on main thread.

Use:

[self updateFilterProgress];

Or use like:

[yourAlert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

Also I checked with your code. There is one error popping up:

Error is: No visible @interface for 'UIAlertView' declares the selector 'performSelectorOnCurrentThread:withObject:waitUntilDone:'

The performSelectorOnMainThread is working perfectly for me.

Try this

- (void) launchActivity
{
 //some logic...
[NSThread detachNewThreadSelector:@selector(updateFilterProgress) toTarget:self withObject:nil];
}
- (void) updateFilterProgress {
 if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
    [self performSelectorOnMainThread: @selector(showAlertForNoNetConnect)];
}
else{
    [self performSelectorOnMainThread: @selector(showAlertForLoading)];
 //some logic...
}

- (void) showAlertForNoNetConnect
{
    UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:@"No Internet Connectivity" message:@"This app require an internet connection via WiFi or cellular network to work." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
    [myAlert show];

}
- (void) showAlertForLoading
{
    UIAlertView *alertMe = [[[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil] autorelease] ;
    [alertMe show];

}

You have to call all the UIKit elements in the main thread. That's what the problem is. Hope this helps. Happy Coding. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM