作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试在 NSOperationQueue 设置的线程上运行 NSTimer
-(void)apiCallbackQueueManager:(NSString *)callid :(NSString *)service:(NSString *)action:(NSString *)data{
SEL theSelector = @selector(apiCallbackQueueTimer: service: action: data:);
NSMethodSignature * sig = [self methodSignatureForSelector:theSelector];
NSInvocation * theInvocation = [NSInvocation invocationWithMethodSignature:sig];
[theInvocation setTarget: self];
[theInvocation setSelector: theSelector];
[theInvocation setArgument: &callid atIndex: 2];
[theInvocation setArgument: &service atIndex: 3];
[theInvocation setArgument: &action atIndex: 4];
[theInvocation setArgument: &data atIndex: 5];
NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithInvocation:theInvocation];
NSOperationQueue *apiQueue = [[NSOperationQueue alloc] init];
[apiQueue addOperation:operation];
}
-(void)apiCallbackQueueTimer:(NSString *)arg1 service:(NSString *)arg2 action:(NSString *)arg3 data:(NSString *)arg4{
apiTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(apiCallbackMonitor:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:arg1, @"callid", arg2, @"service", arg3, @"action", arg4, @"data", nil] repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:apiTimer forMode:NSDefaultRunLoopMode];
}
-(void)apiCallbackMonitor:(NSTimer *)theTimer{
//do something
}
设置调用并运行 NSOperationQueue 似乎没问题,并且使用正确的参数调用了 apiCallbackQueueTimer 方法。问题是我无法让 NSTimer 运行,因此无法使用我的 apiCallbackMonitor 方法。
我在文档中读到 NSTimer 需要一个运行循环,所以我一直在尝试添加一个。
谁能看出我做错了什么?
最佳答案
事实证明,我只需要启动 runloop。
更改 apiCallbackQueueTimer 的最后几行解决了问题。
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:apiTimer forMode:NSRunLoopCommonModes];
[runLoop run];
关于ios - 在 NSOperationQueue 线程中运行 NSTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7715242/
我是一名优秀的程序员,十分优秀!