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:
Using
NSFetchedResultsController
forWorkingDay
:
ThePause
entities are not being observed byNSFetchedResultsController
. TheNSFetchedResultsController
is observingWorkingDay
correctly, but this doesn’t automatically refresh theDetailView
when changes occur inPause
.Calling
objectWillChange.send()
onWorkingDay
:
I callmodel.objectWillChange.send()
after updating thePause
, but this doesn’t seem to trigger a UI refresh for thePause
entities.Using
.id()
on theList
:
Adding.id(UUID())
to theList
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.Explicitly refreshing the
WorkingDay
:
After editing thePause
, I tried manually fetching theWorkingDay
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
版权声明:本文标题:swift - SwiftUI Core Data View not updating after editing related Entity in Modal View - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281085a1926206.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论