Error: Publishing changes from within view updates is not allowed, this will cause undefined behavior

57 Views Asked by At

User /Users/saman/Desktop/new/garageredo/garageredo/garageredoApp.swift:19 Publishing changes from within view updates is not allowed, this will cause undefined behavior.

I get this message when I zoom out on a map on this screen. Not sure the case or what is causing it.

import SwiftUI
import MapKit

struct Home: View {
    @ObservedObject var garageSaleManager = GarageSaleManager()
    @State private var userRegion = MKCoordinateRegion()
    @State private var trackingMode: MapUserTrackingMode = .follow
    @State private var selectedGarageSale: GarageSale?

    var body: some View {
        ZStack(alignment: .bottom) { // Align children to the bottom
            Map(coordinateRegion: $userRegion, showsUserLocation: true, userTrackingMode: $trackingMode, annotationItems: garageSaleManager.garageSales) { garageSale in
                MapAnnotation(coordinate: garageSale.coordinate) {
                    Button(action: {
                        self.selectedGarageSale = garageSale
                    }) {
                        Image(systemName: "house.fill") // Example icon
                            .foregroundColor(.red)
                            .padding(6)
                            .background(Circle().fill(Color.white))
                    }
                }
            }
            .edgesIgnoringSafeArea(.top)
            .onAppear {
                garageSaleManager.fetchGarageSales()
            }

            // Conditionally display DetailBoxView
            if let selectedGarageSale = selectedGarageSale {
                DetailBoxView(garageSale: selectedGarageSale)
                    .padding() // Add padding around the box for better appearance
                    .transition(.move(edge: .bottom)) // Animate the view coming from the bottom
                    .onTapGesture {
                        self.selectedGarageSale = nil // Dismiss the view when tapped
                    }
            }
        }
        .gesture(DragGesture().onChanged({ _ in
            trackingMode = .none // Disable following on user interaction
        }))
    }
}

struct Home_Previews: PreviewProvider {
    static var previews: some View {
        Home()
    }
}

The error itself says it comes from:

import SwiftUI
import Firebase

// AppDelegate to configure Firebase
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }
}

// App entry point
@main
struct garageredoApp: App {
    // Use AppDelegate to configure Firebase
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    // Authentication view model to manage login state
    @StateObject var authViewModel = AuthViewModel()

    var body: some Scene {
        WindowGroup {
            NavigationView {
                if authViewModel.isLoggedIn {
                    ContentView()
                } else {
                    Login()
                }
            }
            .navigationViewStyle(StackNavigationViewStyle())
        }
    }
}

// View model to manage the authentication state
class AuthViewModel: ObservableObject {
    @Published var isLoggedIn = false

    init() {
        // Listen to Firebase auth state changes
        Auth.auth().addStateDidChangeListener { [weak self] (auth, user) in
            DispatchQueue.main.async {
                self?.isLoggedIn = (user != nil)
            }
        }
    }
}

I tried changing map stuff but nothing is working.

0

There are 0 best solutions below