添加计时器后命令行工具不会终止

时间:2014-01-17 16:27:44

标签: ios nstimer nsrunloop

我正在编写一个小命令行工具,它使用AFHTTPNetworkng从服务器中提取一些数据并显示它。但是,由于AFHTTPNetwotking使用异步回调,因此应用程序在从服务器获取响应之前退出。我在那里添加了

NSRunLoop *run = [NSRunLoop currentRunLoop];
_timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(check) userInfo:nil repeats:YES];
[run addTimer:_timer forMode:NSDefaultRunLoopMode];
[run runUntilDate:[NSDate distantFuture]];

此计时器调用check方法,该方法检查BOOL是否已设置为yes(这在回调中发生)。如果有,它会使计时器无效。

- (void)check{

NSLog(@"checking");

if (test == YES) {
    [_timer invalidate];

}

return;
}

此代码工作正常,但现在应用程序没有终止,表明runloop仍在运行。我可以使用exit()来杀死应用程序,但这似乎不可接受,因为这可能会导致内存泄漏。

0 个答案:

没有答案
相关问题