2016-1-21-runloop Use

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.