gpt4 book ai didi

ios - 在 NSOperationQueue 线程中运行 NSTimer

转载 作者:可可西里 更新时间:2023-11-01 06:23:48 27 4
gpt4 key购买 nike

我正在尝试在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com