iOS 線程保活

1、線程?;罟芾眍?lèi).h文件

//

//? ZFPermenantThread.h

//? ZFThread

//

//? Created by zzf on 2020/5/26.

//? Copyright ? 2020 zzf. All rights reserved.

//

#import

NS_ASSUME_NONNULL_BEGIN

@interface ZFPermenantThread : NSObject

- (void)run;

- (void)stop;

- (void)executeTask:(void(^)(void))task;

@end

NS_ASSUME_NONNULL_END

2、線程保活管理類(lèi).m文件

//

//? ZFPermenantThread.m

//? ZFThread

//

//? Created by zzf on 2020/5/26.

//? Copyright ? 2020 zzf. All rights reserved.

//

#import "ZFPermenantThread.h"

@interface ZFThread : NSThread

@end

@implementation ZFThread

- (void)dealloc {

? ? NSLog(@"%s",__func__);

}

@end

@interface ZFPermenantThread ()

@property (nonatomic, strong)ZFThread *innerThread;

@property (nonatomic, assign, getter=isStopped)BOOL stopped;

@end

@implementation ZFPermenantThread

- (instancetype)init {

? ? if(self= [superinit]) {

? ? ? ? self.stopped=NO;


? ? ? ? __weaktypeof(self) weakSelf =self;


? ? ? ? self.innerThread= [[ZFThreadalloc]initWithBlock:^{


? ? ? ? ? ? [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc]init] forMode:NSDefaultRunLoopMode];


? ? ? ? ? ? while(weakSelf && !weakSelf.isStopped) {

? ? ? ? ? ? ? ? [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

? ? ? ? ? ? }

? ? ? ? }];


? ? ? ? [self.innerThreadstart];

? ? }

? ? return self;

}

- (void)run{


? ? if(!self.innerThread) {

? ? ? ? return;

? ? }

? ? [self.innerThread start];

}

- (void)executeTask:(void(^)(void))task {


? ? if(!self.innerThread|| !task) {

? ? ? ? return;

? ? }


? ? [self performSelector:@selector(__executeTask:) onThread:self.innerThread withObject:task waitUntilDone:NO];

}

- (void)stop{

? ? if(!self.innerThread) {

? ? ? ? return;

? ? }

? ? [self performSelector:@selector(__stop) onThread:self.innerThread withObject:nil waitUntilDone:YES];

}

- (void)dealloc {

? ? NSLog(@"%s",__func__);

? ? [selfstop];

}

#pragma mark - private methods

- (void)__stop{

? ? self.stopped=YES;

? ? CFRunLoopStop(CFRunLoopGetCurrent());

? ? self.innerThread = nil;

}

- (void)__executeTask:(void(^)(void))task {

? ? task();

}

@end

3、使用調(diào)用

//

//? ViewController.m

//? ZFThread

//

//? Created by zzf on 2020/5/26.

//? Copyright ? 2020 zzf. All rights reserved.

//

#import "ViewController.h"

#import "ZFPermenantThread.h"

@interface ViewController ()

@property (nonatomic, strong)ZFPermenantThread *thread;

@end

@implementation ViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view.


? ? self.thread = [[ZFPermenantThread alloc] init];

//? ? [self.thread run];

}

- (void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event {


? ? [self.thread executeTask:^{

? ? ? ? NSLog(@"執(zhí)行任務(wù)");

? ? }];

}

- (void)test{


}

- (void)stop{

? ? [self.threadstop];

}

- (void)dealloc {

//? ? [self.thread stop];

}

@end

結(jié)束,小記一下,畢竟好腦子不如爛筆頭。。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容