NSOperationQueue 阻塞主线程,UIAccelerometerDelegate 不会触发
我有一段调用 [NSOperationQueue waitUntilAllOperationsAreFinished] 的代码,在队列中,我放置了一个加速度计的传感器检测器。
加速度计委托需要主线程才能响应 didAccelerate 来记录收集的数据。然而,由于队列阻塞了 main 方法,加速度计的委托似乎永远不会触发。我还注意到在 NSOperationQueue 块发生后强制在主线程 (NSOBject PerformSelectorOnMainThread:withObject:waitUntilDone) 上运行的任何内容也永远不会运行。
我是否可以使用另一个选项来允许发生阻塞,但仍然允许加速计代表触发。
下面是一个快速的代码片段:
NSOperation* accelOp = [[AccelerometerOperation alloc] init];
NSOperationQueue* queue = [[NSOperationQueue alloc] init];
[queue addOperation:accelOP];
[queue waitUntilAllOperationsAreFinished];
对于 AccelerometerOperation,我通过修改 isFinished 和 isExecuting 绕过了 start 方法完成时未结束的问题:
-(void) start{
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
NSLog(@"Did accelerate");
}
如果我删除 waitUntilALlOperationsAreFinished,日志将打印“Did Acceleration”。但是,当 waitUntilFinished 运行时,即使在 DidAccelerate 中放置断点也不会触发。
I have a piece of code which calls [NSOperationQueue waitUntilAllOperationsAreFinished], in which the queue, I have placed a sensor detector for the Accelerometer.
The accelerometer delegate requires the Main thread in order to respond to didAccelerate to record the data collected. However, with the Queue blocking the main method, it seems the delegate for the Acclerometer never gets to fire. I've also noticed anything forced to run on the Main thread (NSOBject performSelectorOnMainThread:withObject:waitUntilDone) after the NSOperationQueue block occurs, will also never run.
Is there another option available to me that will allow the blocking to occur, but still allow the Accelerometer delegate to fire off.
Here's a quick snippet of code:
NSOperation* accelOp = [[AccelerometerOperation alloc] init];
NSOperationQueue* queue = [[NSOperationQueue alloc] init];
[queue addOperation:accelOP];
[queue waitUntilAllOperationsAreFinished];
And for AccelerometerOperation, where I have bypassed the problem of having it not end when the start method finishes by modifying isFinished and isExecuting:
-(void) start{
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
NSLog(@"Did accelerate");
}
If I remove the waitUntilALlOperationsAreFinished, the log prints "Did accelerate." However, with the waitUntilFinished running, even putting a break point into DidAccelerate won't trigger.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论