NSOperationQueue Blocking Main Thread, UIAccelerometerDelegate won't fire
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 *)acce开发者_如何学编程lerometer 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.
精彩评论