Performing an action when scanning a barcode using RSBarcodes

I am creating an application that uses QR code scanning using RSBarcodes for Swift . What I'm trying to do in mine ScanViewControlleris to scan the QR code, check what has been scanned, and then scan the scanned data. Currently, when a QR code is detected, my user interface freezes, and soon after receiving an error and a memory dump:

'NSInternalInconsistencyException', reason: "Only executed in the main thread!".

Perhaps this is the wrong place to check the QR code or not the right place to talk, but if not, I wonder where validation and segue should go. My only other requirement is that the check is only performed when a QR code is detected.

class ScanViewController: RSCodeReaderViewController{
    // Class Variables
    var finalObject: IBuiltCode?
    let ObjectHelper = ObjectBuilder() // Service to validate and build valid scanned objects

    override func viewDidLoad() {
        super.viewDidLoad()

        self.focusMarkLayer.strokeColor = UIColor.redColor().CGColor
        self.cornersLayer.strokeColor = UIColor.yellowColor().CGColor

        self.tapHandler = { point in
            println(point)
        }

        self.barcodesHandler = { barcodes in
            for barcode in barcodes {
                println("Barcode found: type=" + barcode.type + " value=" + barcode.stringValue)
                if let builtObject = self.ObjectHelper.validateAndBuild(barcode,
                      scannedData: barcode.stringValue){
                    println("Good object.")
                    self.performQR()
                }
            }
        }
    }

    func performQR(){
        performSegueWithIdentifier("toQR", sender: self)
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "toQR"){
            let QRVC: QRViewController = segue.destinationViewController as! QRViewController
            QRVC.receivedObject = finalObject as? QRObject
        }
    }
}
+4
source share
1 answer

I contacted the developer of RSBarcodes_Swift on this issue . To perform any UI operation, it must be started in the main thread. For example, the segue function should be changed from:

func performQR(){
        self.performSegueWithIdentifier("toQR", sender: self)
}

to

func performQR(){
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.performSegueWithIdentifier("toQR", sender: self)
    })
}

To avoid multiple repetition of scan time, the call self.session.stopRunning()with breakcan be used in a loop barcodesfor processing.

self.barcodesHandler = { barcodes in
    for barcode in barcodes {
        println("Barcode found: type=" + barcode.type + " value=" + barcode.stringValue)
        if let builtObject = self.ObjectHelper.validateAndBuild(barcode,
              scannedData: barcode.stringValue){
            println("Good object.")
            self.finalObject = builtObject
            self.session.stopRunning() // Avoid scanning multiple times
            self.performQR()
            break
        }
    }
}
+8
source

Source: https://habr.com/ru/post/1598739/


All Articles