UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: CoreData MVVM -> how to edit data in a separate view

Forums > SwiftUI

@sweih  

Hi,

I am looking for a best practice to edit data which is stored with CoreData. Given a Model:

class FoodViewModel : ObservableObject {

    let container: NSPersistentContainer
    @Published var data: [Food] = []

    init() {
        container = NSPersistentContainer(name:"coredata")
        container.loadPersistentStores {( description, error) in
            if let error = error {
                print("ERROR LOADING DATA: \(error)")
            } else {
                print("Sucessfully loaded Core Data")
            }
        }
        fetchFood()
    }

    func fetchFood() {
        let request = NSFetchRequest<Food>(entityName: "Food")

        do {
            self.data = try container.viewContext.fetch(request)
        } catch let error {
            print("Error fetching. \(error)")
        }
    }

    func addFood(name:String, amountUnit:String, amount:Int16) {
        let newFood = Food(context:container.viewContext)
        newFood.name = name
        newFood.amount = amount
        newFood.amountunit = amountUnit
        saveData()
    }

    func saveData() {
        do {
          try container.viewContext.save()
          fetchFood()
        } catch let error {
            print("Error saving... \(error)")
        }
    }
}

The data which is stored in the model is display in a list view:

struct FoodListView: View {

    @StateObject var vm = FoodViewModel()
    @State private var isFoodDetailViewPresented:Bool = false

    var body: some View {
        NavigationView{
        List {
            ForEach(vm.data) { food in
                // Question: How should I pass food to FoodDetailView (which is an edit view) and manage updates of the List?
                NavigationLink(destination:FoodDetailView(food:food)) {
                    Text("\(food.name!)")
               }

            }
            .onDelete(perform: deleteItems)
        }
        .toolbar {
            //EditButton()
            Button(action: addItem) {
                Label("Add Item", systemImage: "plus")
            }
        }
        .navigationTitle("Lebensmittel")
        .sheet(isPresented: $isFoodDetailViewPresented, content: {
            AddFoodView()
        })
    }
    }

Please appologize the miss-named isFoodDetailViewPresented. This points to AddFoodView, which is basically the same. However I found no proper way to use just one view for adding and both editing food.

The FoodDetailView which is used for editing is found below, but crashed when I try to save at the moment. I just do not know what to pass and how to initialize the form:

struct FoodDetailView: View {

    @Environment(\.presentationMode) var presentationMode
    @Environment(\.managedObjectContext) private var viewContext

    @ObservedObject var food: Food

    @StateObject var vm = FoodViewModel()
    let grams = [Int](0...1000)

    var body: some View {
        NavigationView {
            Form {

                Section(header:Text("Name des Lebensmittels")) {
                    TextField("Name", text:$food.name.bound)
                }

                Section(header:Text("Portionsgröße")) {

                    Picker("Menge", selection:$food.amount) {
                        ForEach(0..<grams.count, content: { index in
                            Text("\(self.grams[index])").tag(index)
                        })
                    }
                    Picker("Einheit", selection:$food.amountunit) {
                            Text("g").tag(amounts.g)
                            Text("ml").tag(amounts.ml)
                        }

                }

                Button("Save") {

                    do {
                        try viewContext.save()
                    } catch {
                        let nsError = error as NSError
                        fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
                    }

                    self.presentationMode.wrappedValue.dismiss()
                }
            }.navigationTitle("Detail")
        }
    }
}

I guess that I missunderstood parts of the concept. What would be a better solution. By the way: Binding of $food.amountunit and $food.amount does not work either as expected

2      

hi,

i think we will need more code, so i'll only suggest some things for you to consider.

  • the crash on save is most likely because the value of @Environment(\.managedObjectContext) private var viewContext in the FoodDetailView is not set correctly, because a managed object context was never passed in to the sheet. the sheet that presents the FoodDetailView probably needs an .environment modifier to inject the managed object context.
  • the managed object context in FoodDetailView could be retrieved in using its @ObservedObject var food: Food, say with
    // note: this is an optional value, so use viewContext?.save() and so forth
    let viewContext = food.managedObjectContext 

    (i think SwiftUI may still complain at some point about a managed object context)

  • there's no need for FoodDetailView to have its own @StateObject var vm = FoodViewModel(). there is only one Core Data store (each of your view models above creates its own persistentContainer). the viewModel should be passed in from the FoodListView either explicitly as an @ObservedObject, or through the environment.
  • i would place the definition of @StateObject var vm = FoodViewModel() in the app-level, and then pass it either explicitly or through the environment to the app's views.

hope that gets you started,

DMG

2      

@sweih  

Thanks @delawaremathguy. Passing the model explicitely fixed it. Unfortunately I stepped into the next problem, for which I opened a new thread: https://www.hackingwithswift.com/forums/swiftui/initializing-an-edit-form/8745

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.