开发者

problem i loading progress indicator in iphone sdk?

I want to get list from server it is taking some time So at that time I'm displaying progress indicator. My problem sometimes the progress indicator is appearing and some times it is not appearing. The code I used is

-(void)viewWillAppear:(BOOL)animated

{
     [self loadprogressIndicator];  
}

-(void)loadprogressIndicator
{
     MessengerAppDelegate* appDelegate = (MessengerAppDelegate*) [ [UIApplication sharedApplication] delegate];
    [appDelegate showWaitingView];

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

-(void)statusIndicatorForRefresh
{   
    NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self performSelectorOnMainThread:@selector(loadList) withObject:nil waitUntilDone:NO];
    [NSThread exit];
    [pool release];
}

-(void)lo开发者_如何学PythonadList
{   
    MessengerAppDelegate* appDelegate = (MessengerAppDelegate*) [ [UIApplication sharedApplication] delegate];
    customerList = [appDelegate getCustomersArray];
    [theTableView reloadData];
    [appDelegate removeWaitingView];
}

And in appdelegate.m I implemeted these two methods

- (void)showWaitingView 
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    CGRect frame = CGRectMake(90, 190, 32, 32);
    UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];
    [progressInd startAnimating];
    progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    frame = CGRectMake(130, 193, 140, 30);
    UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
    waitingLable.text = @"Processing...";
    waitingLable.textColor = [UIColor whiteColor];
    waitingLable.font = [UIFont systemFontOfSize:20];
    waitingLable.backgroundColor = [UIColor clearColor];
    frame = [[UIScreen mainScreen] applicationFrame];
    UIView *theView = [[UIView alloc] initWithFrame:frame];
    theView.backgroundColor = [UIColor blackColor];
    theView.alpha = 0.7;
    theView.tag = 999;
    [theView addSubview:progressInd];
    [theView addSubview:waitingLable];
    [progressInd release];
    [waitingLable release];
    [window addSubview:[theView autorelease]];
    [window bringSubviewToFront:theView];
    [pool drain];
}

- (void)removeWaitingView 
{
    UIView *v = [window viewWithTag:999];
    if(v) [v removeFromSuperview];

}

Can anyone please help me. Happy Coding..

Thanks in advance.

Praveena..


You are doing all your activity on the main thread. Your app will appear to be blocked because you block the UI thread.

Also what is the use of launching an NSThread if all it does is call a method on your main thread and not even wait for it to complete? You are doing no multithreading at all.

Your app does this:

in main thread:

  • show waiting view
  • do 1 run loop cycle
  • do your data processing
  • hide waiting view

since your data processing is in the main thread you block everything there. You should do the processing in a secondary thread and leave the main thread for UI handling.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜