admin 管理员组

文章数量: 1086019

I've declared a barcode scanner using expo-camera and it seems to work well locally and on android. When testing in test flight on IOS I receive an error when I scan a barcode. I run an api call and then afterward I set my global state object and navigate to a separate navigation screen. I receive the following error when that happens.

NSGenericException: *** -[AVCaptureSession startRunning] startRunning may not be called between calls to beginConfiguration and commitConfiguration

My code is a a bit large but I've trimmed it down and shown it here. Based on the logs I'm guess I need to wait for the camera to close but I'm not entirely sure whats going wrong. I've attached a screenshot of what my logs are saying

  const [hasPermission, setHasPermission] = useState(null);
  const [scanned, setScanned] = useState(false);
  const [isLoading, setIsLoading] = useState(false); 
  const navigation = useNavigation();
  const isFocused = useIsFocused()
  const scannedRef = useRef(false);  

 const setupCameraAndNotifications = async () => {
    const { status } = await Camera.requestCameraPermissionsAsync();
    if (status !== 'granted') {
      Alert.alert(
        "Camera Permission Needed",
        "Pulse requires access to your camera. Please enable camera permissions in your device settings.",
        [{ text: "Cancel", style: "cancel" }, { text: "Open Settings", onPress: () => Linking.openSettings() }]
      );
      setHasPermission(false);
      return;
    }
    setHasPermission(true);
}

const handleBarCodeScanned = ( type, data ) => {
    if(!scannedRef.current){
      scannedRef.current = true;
    setIsLoading(true);
    setScanned(true)
    console.log("Barcode scanned")
    console.log(data)
    const parsedData = parseData(data)
    service.getVenueInfo(parsedData)
      .then(jsonData => {
        if (jsonData?.data?.venue) { //jsonData.action.payload.data.venue
          setIsLoading(false);
          setScanned(false)
          clear();
          load(jsonData)
          setCurrentVenueAndTable(jsonData.data.venue._id,jsonData.data.table?._id)
          console.log(jsonData.data.venue)
          navigation.navigate('Club Main', { stackAdjust:false, name: jsonData.data.venue.venue_name, tabNav: 'Bar Selection' })
        }
        else {
          console.log(JSON.stringify(jsonData, null, 2), "else");
          setIsLoading(false);

          Alert.alert('Not a valid barcode')
        }

      })
      .catch(error => {
        console.log(error)
        Alert.alert('Scan Error', 'Failed to scan barcode or is invalid. Please try again', [{ text: 'OK', onPress: () => {resetScanner() } }])
      });
    //navigation.navigate('Club Main', { barcodeData: data });
  }
  };

return (
    <View style={styles.container}>
       {isFocused && !isLoading && (
      <CameraView
      onBarcodeScanned={({ data,type }) => {
        if (!scannedRef.current) {
          handleBarCodeScanned(type,data)
        }
      }}
        style={StyleSheet.absoluteFillObject}
        barcodeScannerSettings={{
          barcodeTypes: ["qr"],
        }}
      />
    )}
</View>


I've declared a barcode scanner using expo-camera and it seems to work well locally and on android. When testing in test flight on IOS I receive an error when I scan a barcode. I run an api call and then afterward I set my global state object and navigate to a separate navigation screen. I receive the following error when that happens.

NSGenericException: *** -[AVCaptureSession startRunning] startRunning may not be called between calls to beginConfiguration and commitConfiguration

My code is a a bit large but I've trimmed it down and shown it here. Based on the logs I'm guess I need to wait for the camera to close but I'm not entirely sure whats going wrong. I've attached a screenshot of what my logs are saying

  const [hasPermission, setHasPermission] = useState(null);
  const [scanned, setScanned] = useState(false);
  const [isLoading, setIsLoading] = useState(false); 
  const navigation = useNavigation();
  const isFocused = useIsFocused()
  const scannedRef = useRef(false);  

 const setupCameraAndNotifications = async () => {
    const { status } = await Camera.requestCameraPermissionsAsync();
    if (status !== 'granted') {
      Alert.alert(
        "Camera Permission Needed",
        "Pulse requires access to your camera. Please enable camera permissions in your device settings.",
        [{ text: "Cancel", style: "cancel" }, { text: "Open Settings", onPress: () => Linking.openSettings() }]
      );
      setHasPermission(false);
      return;
    }
    setHasPermission(true);
}

const handleBarCodeScanned = ( type, data ) => {
    if(!scannedRef.current){
      scannedRef.current = true;
    setIsLoading(true);
    setScanned(true)
    console.log("Barcode scanned")
    console.log(data)
    const parsedData = parseData(data)
    service.getVenueInfo(parsedData)
      .then(jsonData => {
        if (jsonData?.data?.venue) { //jsonData.action.payload.data.venue
          setIsLoading(false);
          setScanned(false)
          clear();
          load(jsonData)
          setCurrentVenueAndTable(jsonData.data.venue._id,jsonData.data.table?._id)
          console.log(jsonData.data.venue)
          navigation.navigate('Club Main', { stackAdjust:false, name: jsonData.data.venue.venue_name, tabNav: 'Bar Selection' })
        }
        else {
          console.log(JSON.stringify(jsonData, null, 2), "else");
          setIsLoading(false);

          Alert.alert('Not a valid barcode')
        }

      })
      .catch(error => {
        console.log(error)
        Alert.alert('Scan Error', 'Failed to scan barcode or is invalid. Please try again', [{ text: 'OK', onPress: () => {resetScanner() } }])
      });
    //navigation.navigate('Club Main', { barcodeData: data });
  }
  };

return (
    <View style={styles.container}>
       {isFocused && !isLoading && (
      <CameraView
      onBarcodeScanned={({ data,type }) => {
        if (!scannedRef.current) {
          handleBarCodeScanned(type,data)
        }
      }}
        style={StyleSheet.absoluteFillObject}
        barcodeScannerSettings={{
          barcodeTypes: ["qr"],
        }}
      />
    )}
</View>


Share Improve this question edited Apr 1 at 9:07 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Mar 28 at 4:38 davion williamsdavion williams 313 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This was caused by the conditional rendering of the CameraView. Upgrading to the latest version patched this issue.

本文标签: react nativeExpocamera error in test flight IOS testingStack Overflow