在 NSOperationQueue 线程中运行 NSTimer

发布于 2024-12-09 05:20:02 字数 1606 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-12-16 05:20:02

事实证明我只需要启动runloop。

更改 apiCallbackQueueTimer 的最后几行解决了问题。

 NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:apiTimer forMode:NSRunLoopCommonModes];
[runLoop run];

It turns out that I just needed to start the runloop.

Changing the last few lines of the apiCallbackQueueTimer solved the problem.

 NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:apiTimer forMode:NSRunLoopCommonModes];
[runLoop run];
紫竹語嫣☆ 2024-12-16 05:20:02

当您创建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文