View won't update when Core Data object is updated

Hi, I'm pretty new to programming, and I’m having a bit of trouble with Core Data. I have a list view that passes fetched data to a view that contains the individual objects in the list and another view where you can edit the data. However, when I edit the data, it doesn’t update it on the view until I quit and reopen the app.

So how would I go about manually (or preferably automatically) refreshing the view? I’ve heard that @FetchRequest is supposed to automatically refresh the view when any of the data is updated, but that doesn’t seem to be the case (unless of course I somehow missed some crucial line of code).

Any help would be greatly appreciated!

However, when I edit the data, it doesn’t update it on the view until I quit and reopen the app.

Have you updated the data model and reloaded the table ? For sure, this is done when you relaunch app, but maybe not when you have edited data.

You should show the code
  • where you edit data

  • where you use in the other view

in order to see if there is something wrong there.
Thanks for the reply! Here's my code for updating the data

Code Block Swift
    func autosave(note: Note) {
        let updateTitle = self.title
        let updateBodyText = self.bodyText
        let updateDateModified = Date()
        viewContext.performAndWait {
            note.title = updateTitle
            note.bodyText = updateBodyText
            note.dateModified = updateDateModified
            do {
                try viewContext.save()
                print("Note saved!")
            } catch {
                print(error.localizedDescription)
            }
        }
    }


And here's where I fetched the data

Code Block Swift
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(entity: Note.entity(), sortDescriptors: [NSSortDescriptor(key: "dateModified", ascending: false)], predicate: nil)
    var updatedNote: FetchedResults<Note>


Which I passed into the other view, which treats the data like a regular variable (code below is in the second view)

Code Block Swift
struct NoteMenuItem: View {
    var note: FetchedResults<Note>.Element
    var body: some View {
        HStack {
            VStack(alignment: .leading) {
                Text(note.title ?? "")
                    .font(.headline)
                    .padding(.bottom, 1)
                Text(note.bodyText ?? "")
                    .font(.subheadline)
                    .lineLimit(2)
            }
            Spacer()
        }
        .padding(.leading, 25)
        .padding(.trailing, 25)
        .padding(.top, 10)
        .padding(.bottom, 10)
    }
}


I don't think I explicitly reloaded the table, since I assumed it does so itself? Though the data still updates in one of the views correctly.
How do you update the data is my question, I'm still trying to figure that out. I have the UUID of the entry, but I can't enter that into a fetch request to then delete that record? How did you manage to update the code?
View won't update when Core Data object is updated
 
 
Q