TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Progress View- What am I missing

Forums > SwiftUI

I am building an app where I have the ability to Import/Export CSV files. I am trying to get the ProgressView to show while importing/exporting, but can not seem to make it work.

To simplify I have a view with a button that calls the function to do the import

I have defined the state variable "importInProgress"

My View Simplified


 @State private var importInProgress = false 

var body: some View {
        NavigationView {
            Form {
                if selectedURL != nil {
                    VStack {
                        Section ("Import Title Info "){
                            Button(action: {
                                importInProgress = true
                                importCombinedCSV(from: selectedURL!) { success in
                                    if success {
                                        importInProgress = false
                                        showingimportAlert.toggle() // Show the import alert
                                    } else {
                                        importInProgress = false
                                    }
                                }
                            }) {
                                Label("Restore Data", systemImage: "arrow.down.doc")
                            }
                            .buttonStyle(PlainButtonStyle()) // Remove button styling
                            .foregroundColor(importInProgress ? .gray : .blue) // Change button color to indicate activity
                            .disabled(importInProgress) // Disable the button while import is in progress
                        }
                    }
                }
            }
            .navigationTitle("Settings")
            .alert(isPresented: $showingimportAlert) {
                Alert(
                    title: Text(importSuccess ? "Import Complete" : "Import Failed"),
                    message: Text(importSuccess ? "Import was successful" : "Import failed. Please try again."),
                    dismissButton: .default(Text("OK")){
                        // Close the screen when "OK" button is tapped
                        isPresented = false
                    }
                )
            }
        }
       // ProgressView()
        if importInProgress {
            ProgressView()
        }
    }

if I just pass ProgressView above it triggers and I validated the importInProgress is true so how do retrigger the view ... Sorry a newbie here and not sure what else to try. Sure its something simple.

   

Hi, there are several ways, but could overlay be good for you? You can overlay another view, eg form or button.

      .overlay {
          // ProgressView()
          if importInProgress {
              ProgressView()
          }
      }

   

Thanks, but still not go. Not sure what is not getting triggered. If I just do this I see the progress view. I confirmed that importInProgress is set to true.

.overlay {
          ProgressView()
      }

   

One more thinkg I noticed was if I use DispatchQueue I get the progress view indicator, but the import crashes with : Thread 1: EXC_BAD_ACCESS (code=1, address=0x8000000000000010)

 Button(action: {
                            importInProgress = true // Start the progress before the import operation
                            DispatchQueue.global(qos: .background).async {
                                // Perform the import operation on a background queue
                                importCombinedCSV(from: selectedURL!) { success in
                                    DispatchQueue.main.async {
                                        // Update the UI on the main thread after the import operation completes
                                        if success {
                                            importInProgress = false
                                           print("importInProgress 1.1  \(importInProgress)")
                                           showingimportAlert.toggle() // Show the import alert
                                        } else {
                                            // Handle failure
                                            importInProgress = false
                                        }
                                        importInProgress = false // Update the progress after the import operation
                                    }
                                }
                            }
                        }) {
                            Label("Restore Data", systemImage: "arrow.down.doc")
                        }

   

Hi, read this chat.openai.com/share/6650784e-5a00-49b6-bfcd-f2f1d39b85fb

1      

Thanks Martin that was good stuff. I think I am headed in the right direction, but still can't get the progress view to show. Was trying to post the whole view, but my post keep getting disallowed as spam

   

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.