PerformSelector:onThread:withObject:waitUntilDone 이해

1790 단어

performSelector:onThread:withObject:waitUntilDone 이해


코드 직접 보기
//
//  ViewController.m
//  RunLoopDemo
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, assign) BOOL isAborted;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self threadInfo:@"UI"];
    
    _isAborted = NO;
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(newThread:) object:nil];
    [thread start];
//    When performing a selector on another thread, the target must have an active run loop
    [self performSelector:@selector(test:) onThread:thread withObject:nil waitUntilDone:NO];
}

- (void)newThread:(id)obj {
    @autoreleasepool {
        NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
        while (!_isAborted) {
            [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        }
        NSLog(@"    ");
    }
}

- (void)test:(id)obj {
    [self threadInfo:@"test"];
    _isAborted = YES;
}

- (void)threadInfo:(NSString *)info {
    NSLog(@"%@--%@", info, [NSThread currentThread]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end


어떤 사람들은 왜 [self performSelector:@selector(test:) onThread:thread withObject:nil waitUntilDone:NO]; 중의 test: 방법을 통해 _isAborted 상태를 갱신해야 하는지 이해할 수 없다.왜 없어요?원인은 사실 여기에서 selector 방법을 통해 yes로 설정한 이유는 yes로 설정되었지만, 이때 runloop이 있는 라인은 사실 몰랐기 때문이다isNewThreadAborted가 재지정되었습니다.runloop이 작업 이벤트에 깨어나지 않았습니다.그래서 정확한 방법은 selector를 사용하여 Runloop을 깨우는 것이다.또한 [thread start] 방법을 실행할 때 다른 라인에서 실행할 경우 다른 라인이runloop이라는 것을 보증해야 한다는 것을 주의해야 한다.구체적인 사용은 AFNetworking을 참고할 수 있습니다.

좋은 웹페이지 즐겨찾기