티스토리 뷰

네트웍을 통한 데이터 송수신 등 시간이 걸리는 작업 시 thread 및 Activity Indicator사용을 많이 합니다.

performselector로 인디케이터 돌리고 작업시킨 다음 indicator 핸들로 stop를 했더니 일반적인

경우에는 잘 작동하였으나 performselector가 해당 함수를 호출하기 전에 아래 작업이 모두 끝나고

indicator stopAnimation까지 호출되는 상황 발생. 그 늦게 발동한 indicator의 무한 뺑뻉이...

 

그래서 stop도 performselector로 호출.

 

아래는 작업한 소스코드

 

//
//
//  Created by likehood on 12. 1. 30..
//


//in AppDelegate.h
@interface CAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
-(void) threadStartActivityIndicator:(UIActivityIndicatorView*) indi;
-(void) threadStopActivityIndicator:(UIActivityIndicatorView*) indi;
@end

//  AppDelegate.m
-(void) stopActivityIndicator:(UIActivityIndicatorView*) indi
{
    [indi stopAnimating];
    [indi removeFromSuperview];
    NSLog(@"STOP INDICATOR");
}
-(void) startActivityIndicator:(UIActivityIndicatorView*) indi
{
    UIWindow*   appWindow = [UIApplication sharedApplication].keyWindow;
    indi.color = [[UIColor alloc] initWithWhite:0.5 alpha:1];//  initWithRed:1 green:0 blue:0 alpha:1.0 ];
    CGRect rectCenter=appWindow.frame;
    rectCenter.size.height=rectCenter.size.height;
    [indi setFrame:rectCenter];
    [appWindow addSubview:indi];
    [indi bringSubviewToFront:appWindow];
    indi.hidesWhenStopped  = YES;
    [indi startAnimating];
    NSLog(@"START INDICATOR");
}
//in using class  .m

-(void) threadLoadList
{
    UIActivityIndicatorView*      indi=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    CNIAppDelegate* delegate = (CNIAppDelegate*)[[UIApplication sharedApplication] delegate];
    [delegate performSelector:@selector(startActivityIndicator:) onThread:[NSThread mainThread] withObject:indi waitUntilDone:NO];
    //
    // thread code here
    //
   
    [delegate performSelector:@selector(stopActivityIndicator:) onThread:[NSThread mainThread] withObject:indi waitUntilDone:YES];
    [NSThread exit];
}

- (void) loadList
{
    [NSThread detachNewThreadSelector:@selector(threadLoadList) toTarget:self withObject:nil];
}

댓글