diff --git a/.changeset/hungry-trees-raise.md b/.changeset/hungry-trees-raise.md new file mode 100644 index 0000000..9860667 --- /dev/null +++ b/.changeset/hungry-trees-raise.md @@ -0,0 +1,5 @@ +--- +'@capacitor-mlkit/barcode-scanning': patch +--- + +fix(ios): add delay before starting camera session diff --git a/packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift b/packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift index b24cef9..919ede0 100644 --- a/packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift +++ b/packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift @@ -61,7 +61,13 @@ public protocol BarcodeScannerViewDelegate { throw RuntimeError(implementation.plugin.errorCannotAddCaptureOutput) } captureSession.commitConfiguration() - + // `session.startRunning()` should be called after `session.commitConfiguration()` is complete. + // However, occacsionally `commitConfiguration()` runs asynchronously, so when `startRunning()` + // is called, `commitConfiguration()` is still in progress and the state is still `uncommited`. + // This can be reproduced by repeatedly switching or toggling the camera using the plugin demo. + // Adding a 100ms delay ensures that `session.commitConfiguration()` is complete before calling + // `session.startRunning()`. + Thread.sleep(forTimeInterval: 0.1) DispatchQueue.global(qos: .background).async { captureSession.startRunning() }