Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

100% cpu usage after scanning bar code #103

Closed
klinkert0728 opened this issue Mar 13, 2018 · 15 comments
Closed

100% cpu usage after scanning bar code #103

klinkert0728 opened this issue Mar 13, 2018 · 15 comments

Comments

@klinkert0728
Copy link

Hey i was using your library (btw is awesome), and i found an issue with the cpu usage just after i scan a barcode. Running the time profile i was able to found that the issue is in the barcode.

screen shot 2018-03-13 at 16 05 43

screen shot 2018-03-13 at 16 04 23

@vadymmarkov
Copy link
Contributor

Hey @klinkert0728. I've just tested the demo app and wasn't able to re-produce it. Do you use the last version of BarcodeScanner? If so, can you point to the line of code in the library that causes this?

@klinkert0728
Copy link
Author

hey @vadymmarkov thanks in advance for the quickly replay.
i'm using version 2.1.2 since the code is still running in swift 3

i saved the logs from the profile since i was not able to find the exact line, let me know if that helps

barcode.trace.zip

@vadymmarkov
Copy link
Contributor

We found potential reason for this problem and it should be fixed starting from version 4. But yes, it's in Swift 4.

@klinkert0728
Copy link
Author

klinkert0728 commented Mar 13, 2018

oh can you point me to the solution i can try to make it work for swift 3? , since for now swift 4 is not an option

@vadymmarkov
Copy link
Contributor

As I remember it was about calling captureSession.stopRunning() at some point. Can you post your code to see how you present/dismiss barcode scanner controller?

@klinkert0728
Copy link
Author

private func presentBarCodeScanner() {
    
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
        let scanViewController = BarcodeScannerController()
        scanViewController.codeDelegate         = self
        scanViewController.dismissalDelegate    = self
        scanViewController.errorDelegate        = self
        self.container?.present(scanViewController, animated: true, completion: nil)
    }else {
        let alertController = UIAlertController.getAlertView(title: "This device doesn't have camera", message: nil, noopTitle: "OK")
        self.container?.present(alertController, animated: true, completion: nil)
        
        return
    }
}

// MARK: - scan view controller


func barcodeScanner(_ controller: BarcodeScannerController, didCaptureCode code: String, type: String) {
    controller.dismiss(animated: true, completion: nil)
    subjectTextField?.text = code
    
}

func barcodeScanner(_ controller: BarcodeScannerController, didReceiveError error: Error) {
    print(error.localizedDescription)
}

func barcodeScannerDidDismiss(_ controller: BarcodeScannerController) {
    controller.dismiss(animated: true, completion: nil)
}

@vadymmarkov
Copy link
Contributor

vadymmarkov commented Mar 13, 2018

Try to add this in BarcodeScannerController:

deinit {
    captureSession.stopRunning()
    NotificationCenter.default.removeObserver(self)
}

@klinkert0728
Copy link
Author

i did this, but did not work.

deinit {
   captureSession.stopRunning()
   NotificationCenter.default.removeObserver(self)
 }

@vadymmarkov
Copy link
Contributor

Ok, try to call controller.resetWithError() before you do controller.dismiss(animated: true, completion: nil)?

@klinkert0728
Copy link
Author

did not work

@vadymmarkov
Copy link
Contributor

Ok, running out of ideas here, but try to remove these three lines:

if whenProcessing {
   self.status = Status(state: .processing)
}

from https://github.com/hyperoslo/BarcodeScanner/blob/2.1.2/Sources/BarcodeScannerController.swift#L387

@klinkert0728
Copy link
Author

it works. thank you very much for your help man. i really appreciate it

@mmatheusmmiguel
Copy link

Hey guys,
This solves the problem, but removes the animation. Is there no other way?

@vadymmarkov
Copy link
Contributor

You don't really need that animation when you dismiss controller, do you?

@mmatheusmmiguel
Copy link

@vadymmarkov The animation that appears is product search and / or product not found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants