One: Resident thread: When a thread is required to handle some time-consuming operations, it can have a runloop. The specific code is as follows:
1. Make sure the Runloop does not exit directly by adding the source to Runloopmode.
Here is a very important point of knowledge, Runloop object if mode is empty, it will be returned directly. In the following code, in the Run method:
[[Nsrunloop Currentrunloop] Addport:[nsport port] Formode:nsdefaultrunloopmode]; // add content to Runloop's mode [[Nsrunloop Currentrunloop] run]; // turn on Runloop, which defaults to default mode.
Ensure that the mode of the Runloop object is not empty, that is, does not exit directly, ensuring that the thread continues to run.
@interfaceViewcontroller () @property (nonatomic, strong) Zzthread*Testthread;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Create your own thread and let it execute the Run methodZzthread *thread = [[Zzthread alloc] initwithtarget:self selector: @selector (Run)Object: nil]; Self.testthread=thread; [Self.testthread start];}- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{[Self performselector: @selector (test) OnThread:self.testThread Withobject:nil waituntildone:no];}- (void) test{NSLog (@"%s-------%@", __func__,[nsthread CurrentThread]);}//Add a runloop to the thread to let the thread "Live"- (void) run{NSLog (@"%s--------------%@", __func__,[nsthread CurrentThread]); //If there is no content in Runloop mode, Runloop will be destroyed directly.[[Nsrunloop Currentrunloop] Addport:[nsport port] Formode:nsdefaultrunloopmode];//add content to Runloop's mode[[Nsrunloop Currentrunloop] run];//turn on Runloop, which defaults to default mode. NSLog (@"-------------Run End-----------");//The front Run method has never ended }@end
2. Adding timers to Runloop can also guarantee runloop to exit directly.
- (void) viewdidload {[Super viewdidload]; //Create your own thread and let it execute the Run methodZzthread *thread = [[Zzthread alloc] initwithtarget:self selector: @selector (test2)Object: nil]; Self.testthread=thread; [Self.testthread start];}- (void) test2{Nstimer*timer = [Nstimer timerwithtimeinterval:2target:self selector: @selector (test) Userinfo:nil Repeats:yes]; [[Nsrunloop Currentrunloop] Addtimer:timer Formode:nsdefaultrunloopmode]; [[Nsrunloop Currentrunloop] run]; }- (void) test{NSLog (@"%s-------%@", __func__,[nsthread CurrentThread]);}
Two: Automatic release of the pool
1.RunLoop life cycle of the auto-release pool
Runloop creates an auto-free pool at the start of Step 1 o'clock, and then places the temporary objects that need to be created in the pool, then destroys the pool at step 6, which is a realease operation for all objects.
That is to say, runloop every lap, into the cycle of sleep, the automatic release of the pool life cycle is also from creation to destruction.
2. So when you create a Runloop object, you should also use an auto-release pool to wrap
//Add a runloop to the thread to let the thread "Live"- (void) run{@autoreleasepool {NSLog (@"%s--------------%@", __func__,[nsthread CurrentThread]); //If there is no content in Runloop mode, Runloop will be destroyed directly.[[Nsrunloop Currentrunloop] Addport:[nsport port] Formode:nsdefaultrunloopmode];//add content to Runloop's mode[[Nsrunloop Currentrunloop] run];//turn on Runloop, which defaults to default mode. NSLog (@"-------------Run End-----------");//The front Run method has never ended }}
2016-1-21-runloop Use