UIFeedback触觉引擎调用次数比激活次数多

fat*_*han 12 ios swift ios10 uifeedbackgenerator

我正在使用UIFeedback Haptic Engineswift 2.3类似:

let generator = UINotificationFeedbackGenerator()
generator.notificationOccurred(.Warning)
Run Code Online (Sandbox Code Playgroud)

let generator = UIImpactFeedbackGenerator(style: .Heavy)
generator.impactOccurred()
Run Code Online (Sandbox Code Playgroud)

今天我遇到了这样一种新的错误,但是找不到问题.你有什么主意吗?

UIFeedbackHapticEngine _deactivate] called more times than the feedback engine was activated
Run Code Online (Sandbox Code Playgroud)

细节:

Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x1863e41c0 __exceptionPreprocess
1  libobjc.A.dylib                0x184e1c55c objc_exception_throw
2  CoreFoundation                 0x1863e4094 +[NSException raise:format:]
3  Foundation                     0x186e6e82c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4  UIKit                          0x18cc43fb8 -[_UIFeedbackEngine _deactivate]
5  UIKit                          0x18cad781c -[UIFeedbackGenerator __deactivateWithStyle:]
Run Code Online (Sandbox Code Playgroud)

Pau*_*bre 15

UIImpactFeedbackGenerator不是线程安全的,所以请确保您generator.impactOccurred()同步调用而不是dispatch_async在另一个异步线程中.


Tal*_*ion 5

调用generator.impactOccurred()将在iOS 11.*上崩溃。您需要在主线程上调用它async

let generator = UIImpactFeedbackGenerator(style: style)
generator.prepare()

DispatchQueue.main.async {
   generator.impactOccurred()
}
Run Code Online (Sandbox Code Playgroud)