admin管理员组

文章数量:1122832

I am working on a SwiftUI app with Core Data, where I have a WorkingDay entity with a one-to-many relationship to a Pause entity. Here’s how the app works:

The DetailView displays the details of a WorkingDay, including a list of related Pause entities.
A modal view is used to edit the details of a WorkingDay and Pause entity, such as its start and finish times.
The problem I’m facing is that when I edit a Pause entity (e.g., change its start or finish time) in the modal view, the changes are successfully saved to Core Data, but the UI does not update immediately. I have to reload the app or re-enter the DetailView to see the changes reflected in the list of pauses.

Interestingly, when I edit a WorkingDay (e.g., its working hours, check-in/check-out times), the UI updates correctly, but when I update only the Pause or both WorkingDay and Pause, the Pause list does not refresh, even though the changes are correctly saved in Core Data.

I am using NSFetchedResultsController to observe WorkingDay but not the Pause entities, which I suspect is part of the issue.

Here’s the relevant code:

struct DetailView: View {
    @Environment(\.dismiss) var dismiss
    @StateObject var viewModel: ViewModel

    var body: some View {
        VStack {
            List {
                // Pause Section
                Section("Pauses") {
                    ForEach(viewModel.model.arrPause, id: \.self) { pause in
                        HStack {
                            Text("Start: \(pause.wrappedStartPause.formatted(date: .omitted, time: .standard))")
                            Text("Finish: \(pause.wrappedFinishPause.formatted(date: .omitted, time: .standard))")
                        }
                    }
                }
            }
        }
        .onAppear {
            viewModel.calculatedWorkingTime()
        }
        .sheet(isPresented: $viewModel.onChange) {
            EditSheet(viewModel: viewModel)
        }
    }
}
class ViewModel: ObservableObject {
    @Published var model: WorkingDay
    @Published var selectedPause: Pause?
    @Published var onChange: Bool = false

    private let persistenceController: PersistenceController

    init(model: WorkingDay, persistenceController: PersistenceController) {
        self.model = model
        self.persistenceController = persistenceController
    }

    func update() {
        if let selectedPause {
            selectedPause.startPause = ... // Updated values
            selectedPause.finishPause = ...
        }

        model.objectWillChange.send()
        persistenceController.save()
    }
}

What I've tried:

  1. Using NSFetchedResultsController for WorkingDay:
    The Pause entities are not being observed by NSFetchedResultsController. The NSFetchedResultsController is observing WorkingDay correctly, but this doesn’t automatically refresh the DetailView when changes occur in Pause.

  2. Calling objectWillChange.send() on WorkingDay:
    I call model.objectWillChange.send() after updating the Pause, but this doesn’t seem to trigger a UI refresh for the Pause entities.

  3. Using .id() on the List:
    Adding .id(UUID()) to the List forces the view to reload, but this is not ideal as it causes the entire view hierarchy to rebuild, which may not be the best approach.

  4. Explicitly refreshing the WorkingDay:
    After editing the Pause, I tried manually fetching the WorkingDay again to ensure it’s up to date, but this did not solve the issue.

The WorkingDay and Pause entities have a standard Core Data relationship.
I use a PersistenceController singleton to manage the Core Data stack.
The modal view correctly updates the Core Data store but doesn’t trigger a UI refresh in the main view.

My question:
How can I update the UI in the DetailView when only a Pause is edited, without having to reload the entire app or manually refresh the WorkingDay entity? Should I set up a separate NSFetchedResultsController for Pause, or is there another approach I should take to ensure the UI reflects changes to the Pause entities immediately?

本文标签: swiftSwiftUI Core Data View not updating after editing related Entity in Modal ViewStack Overflow