標籤:orm reserve 存在 com while nonatomic tom 比較 for
線程常駐,正如其名,我們要實現的事讓一個線程長期存在,不被銷毀。
這時會有人說,那還不簡單嗎。
但是這裡我們要實現的事如何讓線程座椅待命,而且並不是主線程。
首先介紹一下正常情況下的線程使用。
//// 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
IOS RunLoop 常駐線程的實現