PerformSelector:onThread:withObject:waitUntilDone 이해
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을 참고할 수 있습니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.