oc 基础 - Runloop 保活线程

104 阅读1分钟

@interface

@property (noatomoc) NSthred thred; @property (noaotonc) Bool stoped = NO;

@end

@implementation

__ weak typeof(self) weakSelf = self;

self.stoped = NO;

self.thred = [NSThred alloc]initWithBlock:^{ NSRunloop currentRunloop] addPort:port forMode:NSDate distactFuture]; }

[self.thred start];

  • (void)touchBegin { if (!self.thred) return;

}

  • (void)stop { // 在子线程调用stop 才能找到 与之相对应的runloop self performeSlecter:@seletor(stoped) onTHred:self.thread withObject:nil waitUntilDon:yes]; }

//用于停止子线程的runloop

  • (void)stopedThread { // 设置标记为yes self.topped = yes; // 停止runloop CFRunloopStop(CFRunloopGetCurret()); //清空线程 selg.thread= nil;

}

daelloc { [self stop] }

@end