iOS runloop 创建一个和App生命周期相同的线程
2016-07-25 21:21
381 查看
//
// NetWorkRequestThread.m
// test_nstherad_port_02
// Created by jeffasd on 16/7/25.
// Copyright © 2016年 jeffasd. All rights reserved.
// 创建一个和app生命周期相同的线程并一直请求发起网络请求
#import "NetWorkRequest.h"
@interface NetWorkRequest ()
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation NetWorkRequest
+ (instancetype)shareNetRequest{
static NetWorkRequest *_networkRequest = nil;
if (_networkRequest == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_networkRequest = [[self alloc] init];
});
}
return _networkRequest;
}
+ (NSThread *)shareNetworkRequestThread{
static NSThread *_networkRequestThread = nil;
if (_networkRequestThread == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkrRequestThreadEntryPoint:) object:nil];
[_networkRequestThread start];
});
}
return _networkRequestThread;
}
+(void)networkrRequestThreadEntryPoint:(id)__unused object{
[[NSThread currentThread] setName:@"networkRequestThread"];
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
//添加一个port监听让runloop一直处于运行状态 好让thread不被回收
[runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
#if 0
BOOL isRuning = YES;
while (isRuning) {
//此方法添加的runloop可以用CFRunLoopStop(runLoopRef)来停止RunLoop的运行
//子线程中的runmode不能使用NSRunLoopCommonModes
// [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
//一直监听线程是否有消息输入(default模式),有当前线程就开始工作,没有就休眠。进行一次消息轮询,如果没有任务需要处理的消息源,则直接返回
BOOL isRuning = [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
//此方法停止runloop要设置isRuning为NO 不是很方便 使用 CFRunLoopRun(); 方法来代替
NSLog(@"isRuning is %@", isRuning ? @"YES" : @"NO");
}
#endif
//可以很方便暂停runloop循环
CFRunLoopRun();
}
- (void)start{
[self performSelector:@selector(startNetWorkRequest) onThread:[[self class] shareNetworkRequestThread] withObject:nil waitUntilDone:NO];
}
- (void)startNetWorkRequest{
// NSThread *currentThread = [NSThread currentThread];
// NSLog(@"currentThread is %@", currentThread);
// NSLog(@"runLoop is %@", [NSRunLoop currentRunLoop]);
NSLog(@"do Something");
self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(getMessageFormRemoteService) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
- (void)getMessageFormRemoteService{
NSLog(@"currentThread is %@", [NSThread currentThread]);
NSLog(@"service -- ");
}
- (void)cancel{
NSThread *therad = [[self class] shareNetworkRequestThread];
NSLog(@"the thread is %@", therad.executing ? @"YES" : @"NO");
NSLog(@"the thread is %@", therad.finished ? @"YES" : @"NO");
NSLog(@"the thread is %@", therad.cancelled ? @"YES" : @"NO");
NSLog(@"the thread is %@", therad);
[self performSelector:@selector(cancelNetWorkRequest) onThread:[[self class] shareNetworkRequestThread] withObject:nil waitUntilDone:NO];
}
- (void)cancelNetWorkRequest{
NSThread *currentThread = [NSThread currentThread];
NSLog(@"currentThread is %@", currentThread);
CFRunLoopStop([NSRunLoop currentRunLoop].getCFRunLoop);
// CFRunLoopStop(CFRunLoopGetCurrent());
}
@end
// NetWorkRequestThread.m
// test_nstherad_port_02
// Created by jeffasd on 16/7/25.
// Copyright © 2016年 jeffasd. All rights reserved.
// 创建一个和app生命周期相同的线程并一直请求发起网络请求
#import "NetWorkRequest.h"
@interface NetWorkRequest ()
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation NetWorkRequest
+ (instancetype)shareNetRequest{
static NetWorkRequest *_networkRequest = nil;
if (_networkRequest == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_networkRequest = [[self alloc] init];
});
}
return _networkRequest;
}
+ (NSThread *)shareNetworkRequestThread{
static NSThread *_networkRequestThread = nil;
if (_networkRequestThread == nil) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkrRequestThreadEntryPoint:) object:nil];
[_networkRequestThread start];
});
}
return _networkRequestThread;
}
+(void)networkrRequestThreadEntryPoint:(id)__unused object{
[[NSThread currentThread] setName:@"networkRequestThread"];
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
//添加一个port监听让runloop一直处于运行状态 好让thread不被回收
[runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
#if 0
BOOL isRuning = YES;
while (isRuning) {
//此方法添加的runloop可以用CFRunLoopStop(runLoopRef)来停止RunLoop的运行
//子线程中的runmode不能使用NSRunLoopCommonModes
// [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
//一直监听线程是否有消息输入(default模式),有当前线程就开始工作,没有就休眠。进行一次消息轮询,如果没有任务需要处理的消息源,则直接返回
BOOL isRuning = [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
//此方法停止runloop要设置isRuning为NO 不是很方便 使用 CFRunLoopRun(); 方法来代替
NSLog(@"isRuning is %@", isRuning ? @"YES" : @"NO");
}
#endif
//可以很方便暂停runloop循环
CFRunLoopRun();
}
- (void)start{
[self performSelector:@selector(startNetWorkRequest) onThread:[[self class] shareNetworkRequestThread] withObject:nil waitUntilDone:NO];
}
- (void)startNetWorkRequest{
// NSThread *currentThread = [NSThread currentThread];
// NSLog(@"currentThread is %@", currentThread);
// NSLog(@"runLoop is %@", [NSRunLoop currentRunLoop]);
NSLog(@"do Something");
self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(getMessageFormRemoteService) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
- (void)getMessageFormRemoteService{
NSLog(@"currentThread is %@", [NSThread currentThread]);
NSLog(@"service -- ");
}
- (void)cancel{
NSThread *therad = [[self class] shareNetworkRequestThread];
NSLog(@"the thread is %@", therad.executing ? @"YES" : @"NO");
NSLog(@"the thread is %@", therad.finished ? @"YES" : @"NO");
NSLog(@"the thread is %@", therad.cancelled ? @"YES" : @"NO");
NSLog(@"the thread is %@", therad);
[self performSelector:@selector(cancelNetWorkRequest) onThread:[[self class] shareNetworkRequestThread] withObject:nil waitUntilDone:NO];
}
- (void)cancelNetWorkRequest{
NSThread *currentThread = [NSThread currentThread];
NSLog(@"currentThread is %@", currentThread);
CFRunLoopStop([NSRunLoop currentRunLoop].getCFRunLoop);
// CFRunLoopStop(CFRunLoopGetCurrent());
}
@end
相关文章推荐
- Android MaterialProgressDrawable类
- Android studio显示行数
- POJ3221——Apple Tree(线段树,dfs序)
- 42.在应用中打开别的应用
- CoordinatorLayout源码解析,探索Behavior机制的奥秘
- Android之用jadx进行反编译
- Android应用逆向——分析反编译代码之大神器
- Android初学之Activity(一)
- Android测试总结
- ART世界探险(7) - 数组
- Android 远程监控摄像头 移动端+PC端 旧手札变废为宝
- onStart和onResume,onPause和onStop的不同--Android开发艺术探索
- cocos2dx 在Xcode里面 resource 里面文件夹的搜索
- Android Developer Document——APP开发深度好文
- Android socket通信 readline方法阻塞
- Android Espresso浅谈
- Android monkeyrunner整理
- Android之mediarecorder中的方法以及工作流程的过程
- 关于Cocos2d-x中让主角运动的方法
- Toolbar:工具条,SearchView:搜索视图