ios – Modifications to Noticed Object not mirrored (intermittently) when sheet is dismissed


I’m utilizing a category conforming to ObservableObject to retailer areas. I’ve used @AppStorage to persist an array of areas chosen by a person.

class SavedLocations: ObservableObject {
    @AppStorage("Places") var all = [Location]()

    func deleteLocation(at offsets: IndexSet) {
        all.take away(atOffsets: offsets)
    }
}

The category above is first instantiated utilizing @StateObject in a menu that shows chosen areas (if any) and a sheet which can be utilized to seek for new areas so as to add.

struct LocationView: View {
    @Setting(.dismiss) var dismiss
    @ObservedObject var networking: Networking
    @StateObject var savedLocations = SavedLocations()
    
    @State non-public var showingSheet = false
    
    var physique: some View {
        NavigationView{
            VStack {

                // the checklist of all location, not at all times refreshed when up to date utilizing LocationSearch sheet presentation 
                Record {
                    ForEach(savedLocations.all, id: .self) { saved in
                        Button(saved.title) {
                            // use the chosen location 
                            dismiss()
                        }
                    }
                    .onDelete(carry out: savedLocations.deleteLocation)
                }
            }
            .sheet(isPresented: $showingSheet) {
                LocationSearch(networking: networking)
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button{
                        showingSheet.toggle()
                    } label: {
                        Label("New Location", systemImage: "plus")
                    }
                }
            }
            .navigationTitle("Places")
        }
    }
}

If a person searches for a location, they’re introduced with a listing of areas as proven beneath:

struct LocationSearch: View {
    @Setting(.dismiss) var dismiss
    @StateObject var locationService = LocationService()
    @ObservedObject var networking: Networking
    @ObservedObject var savedLocations = SavedLocations()
    @State var showAlert = false
    
    var physique: some View{
        Type {
            ...

            Part {
                Record {
                    ForEach(locationService.searchResults, id: .self) { completionResult in
                        Button("(completionResult.metropolis), (completionResult.nation)") {
                            networking.getCoordinate(addressString: completionResult.metropolis) { coordinates, error in
                                if error == nil {
                                    networking.lastLocation = CLLocation(latitude: coordinates.latitude, longitude: coordinates.longitude)
                                    DispatchQueue.essential.async() {
                                        let newLocation = Location(title: completionResult.metropolis)
                                        
                                        // UPDATES not at all times mirrored when sheet is dismissed
                                        savedLocations.all.append(newLocation)
                                        savedLocations.all = savedLocations.all.distinctive()
                                        
                                        dismiss()
                                    }
                                } else {
                                    print("Error setting customized location: (String(describing: error))")
                                    showAlert.toggle()
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

The issue is that upon dismissing the sheet used to seek for and add areas, the brand new location doesn’t at all times instantly seem within the checklist of all areas. Normally it does seem instantly after the search sheet is dismissed, however sometimes it’s essential to dismiss each sheets and set off a brand new occasion of the sheet containing the checklist of all areas.

I based mostly my method off of this publish.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles