RunLoop 02 - 應(yīng)用(線程崩疲活)
PermanentThread
@interface PermanentThread : NSObject
@property (readonly, getter=isExecuting) BOOL executing;
@property (readonly, getter=isFinished) BOOL finished;
- (void)executeTask:(void (^)(void))task;
- (void)stop;
@end
@interface PermanentThread ()
@property (nonatomic, strong) NSThread *thread;
@property (nonatomic, assign, getter=isKeepRunning) BOOL keepRunning;
@end
@implementation PermanentThread
- (void)dealloc {
NSLog(@"%s", __func__);
if (self.isExecuting) {
[self stop];
}
}
- (BOOL)isExecuting {
return self.thread.isExecuting;
}
- (BOOL)isFinished {
return self.thread.isFinished || self.thread.isCancelled;
}
- (instancetype)init {
if (self = [super init]) {
self.keepRunning = true;
__weak typeof(self) weakSelf = self;
self.thread = [[NSThread alloc] initWithBlock:^{
NSLog(@"---- PermanentThread Begin ----");
// ----------------- CFRunLoop -----------------
// 1. 創(chuàng)建 Source、添加 Source
CFRunLoopSourceContext context = {0};
CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
CFRelease(source);
// 2. 啟動 RunLoop渠旁,循環(huán)執(zhí)行
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, false);
// ----------------- NSRunLoop -----------------
// // 1. 添加 Source
// [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
// // 2. 啟動 RunLoop,循環(huán)執(zhí)行
// while (weakSelf && weakSelf.isKeepRunning) {
// [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
// }
NSLog(@"---- PermanentThread End ----");
}];
[self.thread start];
}
return self;
}
- (void)executeTask:(void (^)(void))task {
if (self.isExecuting) {
[self performSelector:@selector(__executeTask:) onThread:self.thread withObject:task waitUntilDone:NO];
}
}
- (void)stop {
if (self.isExecuting) {
[self performSelector:@selector(__stopRunLoop) onThread:self.thread withObject:nil waitUntilDone:YES];
}
}
- (void)__executeTask:(void (^)(void))task {
task();
}
- (void)__stopRunLoop {
self.keepRunning = NO;
CFRunLoopStop(CFRunLoopGetCurrent());
}
@end
PermanentThread 使用示例
@interface ViewController ()
@property (nonatomic, strong) PermanentThread *thread;
@end
@implementation ViewController
- (void)dealloc {
NSLog(@"%s", __func__);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.thread = [[PermanentThread alloc] init];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.thread executeTask:^{
NSLog(@"%@船逮,執(zhí)行任務(wù)顾腊。。挖胃。", [NSThread currentThread]);
}];
}
- (IBAction)stopButtonClicked:(id)sender {
[self.thread stop];
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者