When a thread is created and it is expected to exist, but often the thread we create is stopped after execution and cannot be reused again, then how to create a thread that will allow him to work again, this time it will need to use the runloop. The following is an example of what I wrote:
#import "LongThreadDemoController.h" @interface Longthreaddemocontroller () @property (nonatomic, strong) Nsthread * thread; @end @implementation longthreaddemocontroller-(void) viewdidload {[Super viewdidload]; Self.view.backgroundColor = [Uicolor Whitecolor]; Self.title = @ "resident thread Demo"; }-(void) Threadrunlooppoint: (ID) __unused object{NSLog (@ "%@", Nsstringfromselector (_cmd)); @autoreleasepool {[[Nsthread CurrentThread] setname:@ "Changzhuthread"]; Nsrunloop *runloop = [Nsrunloop currentrunloop]; Here the main is to listen to a port, the purpose is to make this Thread does not reclaim [Runloop Addport:[nsmachport Port] Formode:nsdefaultrunloopmode]; [Runloop Run]; }}-(Nsthread *) thread{if (!_thread) {_thread = [[Nsthread alloc] initwithtarget:self selector: @selector (THR Eadrunlooppoint:) Object:nil]; [_thread start]; } return _thread; }-(void) test{NSLog (@ "%s", __func__);} -(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[sElf Performselector: @selector (test) onThread:self.thread Withobject:nil waituntildone:no modes:@[ Nsdefaultrunloopmode]]; }
Create a resident thread on iOS