Swiftui List View Not Updating After Core Data Entity Updated in Another View

Updated core data values not Reflecting in SwiftUI List

Call

getTaxCodes()

When you want to see an update.

No part of your code is observing the persistent store. Your list doesn’t know when to get the new values

You completely disconnect from CoreData with the View Model and struct. If you are doing this for abstraction and not unintentionally it’s fine but you have to somehow listen to the store so you can recreate everything.

Using something in the View like onChange may or may not provide updates when needed. The store can change from many different directions that your abstraction layer will be unaware of.

This may offer you an alternative for listening.

If this is an unintentional pattern use the CoreData object directly by wrapping it with an

@ObservedObject

That way you can make changes and see them right away.

The link above also has a more direct setup for CoreData and has a simple

CoreDataPersistence

Object that does all the heavy lifting for any object.

SwiftUI List not updating when core data property is updated in other view

NSManagedObject is a reference type so when you change its properties your documents is not changed, so state does not refresh view.

Here is a possible approach to force-refresh List when you comes back

  1. add new state
@State var documents: [ScanDocument] = []
@State private var refreshID = UUID() // can be actually anything, but unique

  1. make List identified by it
List(documents, id: \.id) { item in
ZStack {
DocumentCell(document: item)
}
}.id(refreshID) // << here

  1. change refreshID when come back so forcing List rebuild
NavigationLink(destination: RenameDocumentView(document: documents[selectedDocumentIndex!])
.onDisappear(perform: {self.refreshID = UUID()}),
isActive: $pushActive) {
Text("")
}.hidden()

Alternate: Possible alternate is to make DocumentCell observe document, but code is not provided so it is not clear what's inside. Anyway you can try

struct DocumentCell: View {
@ObservedObject document: ScanDocument

...
}

Views observing a Core Data object are not updating

Instead of didSet, which will not fire if just a property of your object changes, you could observe its changes via objectWillChange (note inline comments as well):

Toggle(isOn: $status, label: {
ControlTitle(title)
})
.onReceive(config.objectWillChange) { _ in
//run the code from your didSet here
//note that you *may* need to use something like DispatchQueue.main.async { } in here to get the updated values of the object since the values may not be updated until the next run loop -- I'm unsure of how this will behave with CoreData objects
}
.onChange(of: status, perform: { newStatus in
//etc

SwiftUI: Array Not Updating In All Views When Referencing The Same Observed Object

Ok I got it working. All I had to do was move the functions I had in my view to my viewModel. Im guessing to keep everything in the same scope. My guess is that everything in ContentView was it's own copy. I'm not 100% certain on that, but it works now.

SwiftUI - View not updating after adding linked object in Coredata

I figured this out and figured I would post my solution in case anyone else had the same problem. I was able to solve this just by adding @ObservedObject in front of var instance: Instance.

SwiftUI: List does not update automatically after deleting all Core Data Entity entries

The reason is that execute (as described in details below - pay attention on first sentence) does not affect managed objects context, so all fetched objects remains in context and UI represents what is really presented by context.

So in general, after this bulk operation you need to inform back to that code (not provided here) force sync and refetch everything.

API interface declaration

// Method to pass a request to the store without affecting the contents of the managed object context.
// Will return an NSPersistentStoreResult which may contain additional information about the result of the action
// (ie a batch update result may contain the object IDs of the objects that were modified during the update).
// A request may succeed in some stores and fail in others. In this case, the error will contain information
// about each individual store failure.
// Will always reject NSSaveChangesRequests.
@available(iOS 8.0, *)
open func execute(_ request: NSPersistentStoreRequest) throws -> NSPersistentStoreResult

For example it might be the following approach (scratchy)

// somewhere in View declaration
@State private var refreshingID = UUID()

...
// somewhere in presenting fetch results
ForEach(fetchedResults) { item in
...
}.id(refreshingID) // < unique id of fetched results

...

// somewhere in bulk delete
try context.save() // < better to save everything pending
try context.execute(deleteRequest)
context.reset() // < reset context
self.refreshingID = UUID() // < force refresh


Related Topics



Leave a reply



Submit