IOS Runloop resident Thread implementation

Source: Internet
Author: User

The thread is resident, as its name is, what we want to achieve is to let a thread exist for a long time and not be destroyed.

Then someone will say, that's not easy.

But here's what we're going to do to get the thread seat on standby and not the main thread.

Let's start with the normal usage of threads.

  viewcontroller.m//  CX runloop resident thread implementation////  Created by Ma C on 16/3/30.//  Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h" #import "CXThread.h" @interface Viewcontroller () @property (nonatomic, Strong) Cxthread * thread; @end @implementation viewcontroller-(void) viewdidload {    [super viewdidload];     nsthread* thread = [[Cxthread alloc]initwithtarget:self selector: @selector (run) Object:nil];        [Thread start];} -(void) run{        NSLog (@ "Run--Xu Bao love to eat fish");    } -(void) test{        NSLog (@ "Test--Xu Bao love to eat fish%@", [Nsthread CurrentThread]);    } -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{        [self test];        Let the test method be implemented on thread threads//    [self performselector: @selector (test) onthread:_thread Withobject:nil Waituntildone: Nil];    } @end

The above code knowledge is simple to implement the use of threads.

Here is its (note the destruction of threads)

Actually, test is not related to thread.

My knowledge simply lets it output the default mainline logs for later comparisons.

Here's how to make thread a global variable

  viewcontroller.m//  CX runloop resident thread implementation////  Created by Ma C on 16/3/30.//  Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h" #import "CXThread.h" @interface Viewcontroller () @property (nonatomic, Strong) Cxthread * thread; @end @implementation viewcontroller-(void) viewdidload {    [super viewdidload];     _thread = [[Cxthread alloc]initwithtarget:self selector: @selector (run) Object:nil];        [_thread start];} -(void) run{        NSLog (@ "Run--Xu Bao love to eat fish");    } -(void) test{        NSLog (@ "Test--Xu Bao love to eat fish%@", [Nsthread CurrentThread]);    } -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{        [self test];        Let the test method be implemented on thread threads//    [self performselector: @selector (test) onthread:_thread Withobject:nil Waituntildone: Nil];    } @end

Can be found by us. Thread has not been destroyed. And test, is still implemented on the main thread.

But what we want is that test is implemented on thread (actual development is not allowing time-consuming operations in the main thread)

Let's make Test in thread: (Note that the shrimp method is not successful)

  viewcontroller.m//  CX runloop resident thread implementation////  Created by Ma C on 16/3/30.//  Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h" #import "CXThread.h" @interface Viewcontroller () @property (nonatomic, Strong) Cxthread * thread; @end @implementation viewcontroller-(void) viewdidload {    [super viewdidload];     _thread = [[Cxthread alloc]initwithtarget:self selector: @selector (run) Object:nil];        [_thread start];} -(void) run{        NSLog (@ "Run--Xu Bao love to eat fish");    } -(void) test{        NSLog (@ "Test--Xu Bao love to eat fish%@", [Nsthread CurrentThread]);    } -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{    //    Let the test method be implemented on thread threads    [Self performselector: @selector (test) Onthread:_thread Withobject:nil Waituntildone:yes];    } @end

Why is it not successful?? (I really clicked)

The reason is that we simply built a thread ... Very simple ... Think about what we should do.

Then we have two ways to achieve this.

Method One (more normal method)

  viewcontroller.m//  CX runloop resident thread implementation////  Created by Ma C on 16/3/30.//  Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h" #import "CXThread.h" @interface Viewcontroller () @property (nonatomic, Strong) Cxthread * thread; @end @implementation viewcontroller-(void) viewdidload {    [super viewdidload];     _thread = [[Cxthread alloc]initwithtarget:self selector: @selector (run) Object:nil];        [_thread start];} -(void) run{        NSLog (@ "Run--Xu Bao love to eat fish");    Add port real-time listening    [[Nsrunloop Currentrunloop] Addport:[nsport port] Formode:nsdefaultrunloopmode];    Add Runloop    [[Nsrunloop currentrunloop]run];        } -(void) test{        NSLog (@ "Test--Xu Bao love to eat fish%@", [Nsthread CurrentThread]);    } -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{    //    Let the test method be implemented on thread threads    [Self performselector: @selector (test) OnThread:self.thread Withobject:nil Waituntildone:no];    } @end

It's that simple.

Method Two

  viewcontroller.m//  CX runloop resident thread implementation////  Created by Ma C on 16/3/30.//  Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h" #import "CXThread.h" @interface Viewcontroller () @property (nonatomic, Strong) Cxthread * thread; @end @implementation viewcontroller-(void) viewdidload {    [super viewdidload];     _thread = [[Cxthread alloc]initwithtarget:self selector: @selector (run) Object:nil];        [_thread start];} -(void) run{        NSLog (@ "Run--Xu Bao love to eat fish");        while (1) {        //Add Runloop        [[Nsrunloop currentrunloop]run];}    } -(void) test{        NSLog (@ "Test--Xu Bao love to eat fish%@", [Nsthread CurrentThread]);    } -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{    //    Let the test method be implemented on thread threads    [Self performselector: @selector (test) OnThread:self.thread Withobject:nil Waituntildone:no];    } @end

IOS Runloop The implementation of a resident thread

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.