IOS RunLoop 常駐線程的實現


線程常駐,正如其名,我們要實現的事讓一個線程長期存在,不被銷毀。

這時會有人說,那還不簡單嗎。

但是這里我們要實現的事如何讓線程座椅待命,而且並不是主線程。

首先介紹一下正常情況下的線程使用。

//
//  ViewController.m
//  CX RunLoop 常駐線程的實現
//
//  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 -- 旭寶愛吃魚");
    
}
-(void)test{
    
    NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self test];
    
    //讓test方法在線程thread上實現
//    [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:nil];
    
}
@end

上面的代碼知識簡單的實現了線程的使用。

下面是其效果圖(注意線程的銷毀)

實際上test與thread並沒有關系。

我知識簡單的讓其輸出默認的主線程日志,以供后面對比。

下面是讓thread為全局變量

//
//  ViewController.m
//  CX RunLoop 常駐線程的實現
//
//  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 -- 旭寶愛吃魚");
    
}
-(void)test{
    
    NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self test];
    
    //讓test方法在線程thread上實現
//    [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:nil];
    
}
@end

由效果圖我們可以發現。thread並沒有銷毀。而且test,依舊是在主線程上實現的。

但我們想要的是test在thread上實現(實際開發中是不允許耗時操作在主線程中的)

我們讓test在thread中實現:(注意蝦米那方法並不成功)

//
//  ViewController.m
//  CX RunLoop 常駐線程的實現
//
//  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 -- 旭寶愛吃魚");
    
}
-(void)test{
    
    NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    

//    讓test方法在線程thread上實現
    [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:YES];
    
}
@end

為什么會不成功呢??(我真的點擊了)

原因是我們只是單純的建立了一個線程。。。很單純的。。。考慮一下我們該怎么做。

那么我們有兩種做法實現。

方法一(比較正常的方法)

//
//  ViewController.m
//  CX RunLoop 常駐線程的實現
//
//  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 -- 旭寶愛吃魚");
    //添加Port 實時監聽
    [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
    //添加runloop
    [[NSRunLoop currentRunLoop]run];
    
    
}
-(void)test{
    
    NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    

//    讓test方法在線程thread上實現
    [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];
    
}
@end

就是這么簡單。

方法二

//
//  ViewController.m
//  CX RunLoop 常駐線程的實現
//
//  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 -- 旭寶愛吃魚");
    
    while (1) {
        //添加runloop
        [[NSRunLoop currentRunLoop]run];
    }
}
-(void)test{
    
    NSLog(@"test -- 旭寶愛吃魚 %@",[NSThread currentThread]);
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    

//    讓test方法在線程thread上實現
    [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];
    
}
@end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM