Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retry action not triggering alert again

I’ve been trying to create a function that can be retried after showing an alert, but for some reason the second time the function runs, the alert is not showing up. I would like to have the throwing function be rerun on the retry action.

import SwiftUI

struct SplashScreen: View {
    @State private var shouldPresentAlert = false

    var body: some View {
        ProgressView()
            .onAppear {
                performPotentiallyThrowingFunction()
            }
            .alert("Thrown", isPresented: $shouldPresentAlert) {
                Button("Retry", role: .cancel) {
                    performPotentiallyThrowingFunction()
                }
            } message: {
                Text("Something went wrong")
            }
    }

    private func performPotentiallyThrowingFunction() {
        do {
            throw URLError(.badURL)
        } catch {
            shouldPresentAlert = true
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The alert isn’t getting closed yet, so setting it to true again doesn’t have any effect.

To have it work as expected, you can close the Alert by setting shouldPresentAlert to false and then, on the next run loop or later, setting it to true

struct SplashScreen: View {
    @State private var shouldPresentAlert = false

    var body: some View {
        ProgressView()
            .onAppear {
                performPotentiallyThrowingFunction()
            }
            .alert("Thrown", isPresented: $shouldPresentAlert) {
                Button("Retry", role: .cancel) {
                    shouldPresentAlert = false
                    Task {
                        performPotentiallyThrowingFunction()
                    }
                }
            } message: {
                Text("Something went wrong")
            }
    }

    private func performPotentiallyThrowingFunction() {
        do {
            throw URLError(.badURL)
        } catch {
            shouldPresentAlert = true
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading