Muz
Muz

Reputation: 759

Update Core Data when back button clicked of NavigationView in SwiftUI

User can delete some Core Data in NavigationLink. How to save it when back button clicked and return to NavigationView?

Upvotes: 0

Views: 457

Answers (1)

Frankenstein
Frankenstein

Reputation: 16361

You can do it in the onAppear method of the previous View in the NavigationalStack.

struct ContentView: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    var body: some View {
        Text("")
            .onAppear {
                do {
                    try self.managedObjectContext.save()
                } catch {
                    // handle the Core Data error
                }
        }
    }
}

Upvotes: 1

Related Questions