twostraws / CodeScanner

A SwiftUI view that is able to scan barcodes, QR codes, and more, and send back what was found.
MIT License
1.02k stars 293 forks source link

Navigation links stop working after going back from view with Scanner #44

Closed VladLupashevskyi closed 2 years ago

VladLupashevskyi commented 2 years ago

Here is the example:

import SwiftUI
import CodeScanner

struct Example: View {
    @State private var showQr = false
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: QR(), isActive: $showQr) { Text("Click me") }
            }
        }.navigationViewStyle(StackNavigationViewStyle())
    }
}

struct QR: View {
    var body: some View {
        CodeScannerView(
            codeTypes: [.qr],
            completion: {result in }
        )
    }
}

If you click on Click me and then immediately press Back (this might require a couple of tries to reproduce) then suddenly nothing happens after clicking on Click Me, so the app needs to be restarted.

I noticed that I cannot reproduce this behaviour if I remove either isActive from NagivationLink or .navigationViewStyle(StackNavigationViewStyle()).

I believe it's some kind of race happening and also I think I've had bigger chance to reproduce it, because I have iPhone 7 which is slower than newer ones.

I've tried to apply changes mentioned in #31 by @breyed and it fixed the issue on my side.

DispatchQueue.global(qos: .userInitiated).async {
    self.captureSession.startRunning()
}
DispatchQueue.global(qos: .userInitiated).async {
    self.captureSession.stopRunning()
}
nathanfallet commented 2 years ago

As mentioned in #31, this was fixed on Dec 14th.