-------------------------iOS-NSRunLoop編程詳解1---------------------------------
編程
NSLog(@"我是主線程的NSRunloop%@",[NSRunLoop currentRunLoop]); [NSThread detachNewThreadSelector:@selector(newThread:) toTarget:self withObject:nil]; } -(void)newThread:(id)sender{ [[NSRunLoop currentRunLoop] run]; NSLog(@"我是子線程的NSRunloop%@",[NSRunLoop currentRunLoop]); }
2015-01-11 14:45:58.839 GCDTest[716:31530]我是主線程的NSRunloop<CFRunLoop 0x7fd399d07280 [0x1074c89a0]>。。。。。。。安全
2015-01-11 14:45:58.861 GCDTest[716:31584]我是子線程的NSRunloop<CFRunLoop 0x7fd399e2e2a0 [0x1074c89a0]>。。。。。。。多線程
函數介紹:函數
//添加一個定時器,當定時器的時間到時就會觸發定時器制定的函數oop
- (void)addTimer:(NSTimer *)timer forMode:(NSString *)mode;spa
//添加一個NSPort,當NSPort對象有消息就會觸發NSPort的代理方法
線程
- (void)addPort:(NSPort *)aPort forMode:(NSString *)mode;代理
//移除端口code
- (void)removePort:(NSPort *)aPort forMode:(NSString *)mode;component
//子線程的NSRunLoop是沒有啓動的,因此要經過該函數來啓動NSRunLoop
- (void)run;
- (void)viewDidLoad { [super viewDidLoad]; NSTimer *timer = [[NSTimer alloc]initWithFireDate:[NSDate date] interval:3 target:self selector:@selector(timerFire:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; } -(void)timerFire:(id)sender{ NSLog(@"i am fired"); }運行結果:相差三秒左右
2015-01-11 15:10:06.770 GCDTest[812:39356] i am fired
2015-01-11 15:10:09.758 GCDTest[812:39356] i am fired
2015-01-11 15:10:12.758 GCDTest[812:39356] i am fired
2015-01-11 15:10:15.759 GCDTest[812:39356] i am fired
2015-01-11 15:10:18.759 GCDTest[812:39356] i am fired
2015-01-11 15:10:21.759 GCDTest[812:39356] i am fired
2015-01-11 15:10:24.758 GCDTest[812:39356] i am fired
- (void)viewDidLoad { [super viewDidLoad]; [NSThread detachNewThreadSelector:@selector(newThread:) toTarget:self withObject:nil]; } -(void)newThread:(id)sender{ NSTimer *timer = [[NSTimer alloc]initWithFireDate:[NSDate date] interval:3 target:self selector:@selector(timerFire:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] run]; } -(void)timerFire:(id)sender{ NSLog(@"i am fired"); }
2015-01-11 15:12:53.509 GCDTest[854:40639] i am fired
2015-01-11 15:12:56.514 GCDTest[854:40639] i am fired
2015-01-11 15:12:59.514 GCDTest[854:40639] i am fired
2015-01-11 15:13:02.514 GCDTest[854:40639] i am fired
2015-01-11 15:13:05.513 GCDTest[854:40639] i am fired
2015-01-11 15:13:08.514 GCDTest[854:40639] i am fired
2015-01-11 15:13:11.514 GCDTest[854:40639] i am fired
- (void)viewDidLoad { [super viewDidLoad]; NSMachPort * port = [[NSMachPort alloc]init]; //#1 [port setDelegate:self]; //#2 [[NSRunLoop currentRunLoop] addPort:port forMode:NSRunLoopCommonModes];//#3 [NSThread detachNewThreadSelector:@selector(newThread:) toTarget:self withObject:port]; //#4 } - (void)handleMachMessage:(void *)msg{ //#5 NSLog(@"hello wrod%s",msg); //#6 } -(void)newThread:(NSMachPort *)sender{ NSMachPort *p = [[NSMachPort alloc]init]; //#7 [sender sendBeforeDate:[NSDate distantFuture] components:nil from:p reserved:0];//#8 NSLog(@"subthread=%@",[NSThread currentThread]); }
2015-01-14 22:19:12.519 GCDTest[742:27354] subthread=<NSThread: 0x7ff700f21ed0>{number = 2, name = (null)}
2015-01-14 22:19:12.599 GCDTest[742:27249] hello wrod
代碼說明:
#1:新建一個port,至關於一個線程的耳朵,這時耳朵並無安裝上,
#2:給它設置一個代理,當這個耳朵聽到聲音時,代理要對msg作出相應。
#3:把port加入到當前線程的runloop上,至關於給這個線程的裝上了耳朵
#4:新建一個線程,並告訴這個線程若是你要向當前線程說話,你能夠經過port傳遞消息
#6:當主線程的耳朵port獲得消息時,作相應的處理
#7-#8:你也能夠給該線程裝上一個port耳朵並安裝,你能夠經過sender 傳給主線程信息