2011-02-02 5 views
2

일부 스레드 백그라운드 업데이트가 진행되는 동안 UIView를 추가하고 대리자 메서드를 사용하여 뷰를 제거합니다. 모든 것이 의도 한대로 진행되지만 hideActivityViewer가 호출 된 후 몇 초 동안 뷰가 유지됩니다. 중요한지 확실하지 않지만 앱이 UITabBarController를 사용합니다.UIView가 수퍼 뷰에서 제거되는 속도가 느림

업데이트 방법은 별도의 클래스이지만 현재 디버그 목적으로 AppDelegate.m에 있습니다. 내가 말했듯이, 모든 것이 효과가 있습니다. 업데이트가 완료되면 "Foo"가 로그에 기록되지만 뷰는 몇 초 동안 지속됩니다. 어떤 도움을 주시면 감사하겠습니다. 불필요한 코드는 생략되었습니다

AppDelegate.h

@interface AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> { 
    UIWindow *window; 
    UITabBarController *tabBarController; 
    UIView *activityView; 
    id _delegate; 
} 
@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) UITabBarController *tabBarController; 
- (void)showActivityViewer; 
- (void)updateComplete; 
- (void)updateRemoteDataThreadedWithDelegate:(id)aDelegate; 
- (id)delegate; 
- (void)setDelegate:(id)new_delegate; 
@end 

AppDelegate.m 당신의 UIView에 물건을 수행하는 대리자 메서드를 호출하는 스레드를 일으키는 것처럼 나에게 보이는

- (void)updateRemoteDataThreadedWithDelegate:(id)aDelegate { 
    [self setDelegate:aDelegate]; 
    NSOperationQueue *queue = [NSOperationQueue new]; 
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateRemoteDataWithDelegate:) object:aDelegate]; 
    [queue addOperation:operation]; 
    [operation release]; 
} 

- (void)updateRemoteDataWithDelegate:(id)aDelegate { 
    [self setDelegate:aDelegate]; 
    ...do stuff... 
    if ([_delegate respondsToSelector:@selector(updateComplete)]) { 
    [_delegate updateComplete]; 
    } else { 
    [NSException raise:NSInternalInconsistencyException format:@"Delegate doesn't respond to updateComplete"]; 
    } 
} 

-(void)showActivityViewer { 
    [activityView release]; 
    activityView = [[UIView alloc] initWithFrame: CGRectMake(window.bounds.size.width-50, 60, 50, 50)]; 
    ...formatting... 
    [window addSubview: activityView]; 
    [activityView release]; 
} 
-(void)hideActivityViewer { 
    [activityView removeFromSuperview]; 
    activityView = nil; 
    NSLog(@"Foo"); 
} 

- (id)delegate { 
    return _delegate; 
} 

- (void)setDelegate:(id)new_delegate { 
    _delegate = new_delegate; 
} 

답변

3

UIView는 스레드로부터 안전하지 않기 때문에 그렇게 할 수 없습니다.

performSelectorOnMainThread를 사용하면이 작업을 안전하게 수행 할 수 있습니다. 위임 메서드는 주 스레드에서 다른 메서드를 호출 할 수 있습니다.

+0

감사합니다. 나는 2 일을 낭비하기 전에 여기에서 물어야했음을 짐작한다. 에 딱 맞다. =] – SalsaShark

3

UI 작업은 주 스레드에서 수행해야합니다. 귀하의 예제는 권장하지 않는 별도의 스레드에 UIView를 푸시합니다.

관련 문제