ios – Refresh Listing After a New Entity is Added in Core Information for SwiftUI App


I’m constructing a small app utilizing SwiftUI and Core Information. I’ve a major view, which launches the sheet. The sheet permits me so as to add a brand new film to the SQLite database by means of Core Information. However I’m having a tough time to refresh the dad or mum view as soon as the sheet is dismissed.

ContentView

struct ContentView: View {
    
    @State non-public var isPresented: Bool = false
    @StateObject non-public var vm = MovieListViewModel()
    
    var physique: some View {
        NavigationView {
            VStack {
                Listing(vm.films) { film in
                    Textual content(film.title)
                }
            }
            .navigationTitle("Films")
            .toolbar(content material: {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Add Film") {
                        isPresented = true
                    }
                }
            })
            .sheet(isPresented: $isPresented, content material: {
                AddMovieView() 
            })
            .onAppear {
                strive? vm.populateMovies()
            }.padding()
        }
        
    }
}

AddMovieView

struct AddMovieView: View {
    
    @Atmosphere(.dismiss) non-public var dismiss
    @StateObject non-public var vm = AddMovieViewModel()
    
    var physique: some View {
        Type {
            TextField("Title", textual content: $vm.title)
            Button("Save") {
                do {
                    strive vm.saveMovie()
                    dismiss()
                } catch {
                    print(error.localizedDescription)
                }
            }
        }
    }
}

Do I have to name vm.populateMovies() on the onDismiss perform of the sheet from the ContentView?

Leave a Reply