简体   繁体   English

NSOperationQueue Heisenbug?

[英]NSOperationQueue Heisenbug?

I'm working on an app that calls AudioServicesPlayAlertSound(kSystemSoundID_Vibrate) from a loop in a separate thread launched with performSelectorInBackground . 我正在开发一个应用程序,该应用程序通过用performSelectorInBackground启动的单独线程中的循环从循环调用AudioServicesPlayAlertSound(kSystemSoundID_Vibrate) For some reason, the app only vibrates reliably when the entire app is running in the background. 由于某些原因,仅当整个应用程序在后台运行时,该应用程序才能可靠地振动。 I tried calling the vibrate function from a block sent to the main NSOperationQueue, but it's still only consistent when in the background. 我尝试从发送到主NSOperationQueue的块中调用vibrate函数,但仅在后台时它仍然保持一致。 Unless, of course, I throw a NSLog() call into the operation block: 当然,除非我将NSLog()调用扔到操作块中:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    NSLog(@"VIBRATING");
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
}];

With the NSLog call, it runs as intended regardless of where it's running. 通过NSLog调用,无论在何处运行,它都能按预期运行。

Any idea why this is? 知道为什么会这样吗? All suggestions are much appreciated 所有建议都非常感谢

The header file documentation for AudioServicesPlayAlertSound states AudioServicesPlayAlertSound状态的头文件文档

Play the provided SystemSoundID with AlertSound behavior. 播放具有AlertSound行为的提供的SystemSoundID。

I would imagine that it simply does not play while the app is considered active and front-most. 我可以想象,当该应用程序被认为处于活动状态且位于最前端时,它根本无法播放。 Maybe try using AudioServicesPlaySystemSound instead? 也许尝试改为使用AudioServicesPlaySystemSound

One answer to Making the iPhone vibrate solved my problem. 使iPhone振动的一个答案解决了我的问题。 For some reason, the iPhone doesn't like to vibrate while recording audio. 由于某些原因,iPhone在录制音频时不喜欢振动。 So the obvious solution is to stop recording before vibrating: 因此,显而易见的解决方案是在振动之前停止记录:

[_recorder stop];
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
[_recorder record];

Still, I'm not sure why the NSLog has any effect, and I still don't see why it worked just fine without the above fix while running in the background. 仍然,我不确定为什么NSLog会产生任何效果,并且我仍然不知道为什么在后台运行时,如果没有上述修复,它仍然可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM