在 NSOperationQueue 线程中运行 NSTimer
我正在尝试在 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 需要一个运行循环,所以我一直在尝试添加一个。
谁能看到我做错了什么吗?
I'm trying to run an NSTimer on a thread set up by an NSOperationQueue
-(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
}
Setting up the invocation and running the NSOperationQueue seem to be OK and the apiCallbackQueueTimer method gets called with the right arguments. The problem is that I cannot get the NSTimer to run and so get to my apiCallbackMonitor method.
I've read in the docs that NSTimer requires a run loop, so I've been trying to add one.
Can anyone see what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明我只需要启动runloop。
更改 apiCallbackQueueTimer 的最后几行解决了问题。
It turns out that I just needed to start the runloop.
Changing the last few lines of the apiCallbackQueueTimer solved the problem.
当您创建 NSInitationOperation 时,没有实例化运行循环。这可能会在手术开始时发生。
Soooo,我建议子类化 NSInitationOperation 并在调用子类化的 [NSInitationOperation start] 方法时调用 apiCallbackQueueTimer 选择器。
There's no run loop instantiated when you create that NSInvocationOperation. That likely happens when the operation starts.
Soooo, I'd suggest subclassing NSInvocationOperation and have that apiCallbackQueueTimer selector be called when your subclassed [NSInvocationOperation start] method is called.