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

Completion handler might be called during view update #112

Open
fossil12 opened this issue Jul 31, 2023 · 2 comments
Open

Completion handler might be called during view update #112

fossil12 opened this issue Jul 31, 2023 · 2 comments

Comments

@fossil12
Copy link

  1. Grant camera access for app
  2. Disable camera access
  3. Open view using a navigation link that contains the CodeScannerView which shows an alert using a state boolean if there is not camera access (see code below).

Expected: The alert appears
Actual: No alert is shown and an error is logged: [SwiftUI] Modifying state during view update, this will cause undefined behavior.
Workaround: Set the change to the alert bool in a new main actor task


Code to reproduce the issue:

// ContentView.swift

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink("Show Detail") {
                DetailCameraView()
            }
        }
    }
}
// DetailCameraView.swift

import SwiftUI
import CodeScanner

struct DetailCameraView: View {
    
    @State private var showNoCameraAccess = false
    
    var body: some View {
        CodeScannerView(codeTypes: [.qr]) { result in
            switch result {
            case .success(_):
                print("Success")
            case .failure(_):
                // Task { @MainActor in // Workaround
                    showNoCameraAccess = true
                // }
            }
        }
        .alert("No Camera Access", isPresented: $showNoCameraAccess) {
            // OK button is shown by default
        }
    }
}
@timlittlelabs
Copy link

Can confirm this is happening for us too.

@Jeehut
Copy link
Contributor

Jeehut commented Oct 23, 2024

Same here. Used @fossil12's workaround.

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