Changes to Swiftui Fetchrequest Not Triggering View Refresh

Changes to SwiftUI FetchRequest not triggering view refresh?

Think I figured out why this was happening. Problem is the way I was navigating to the view. Having a navigation link inside the navigation bar items was the cause. Instead I moved the navigation link outside on it's own, and replaced it with a button that controls the active state of the navigation link. Fetch Request seems to work as expected now.

Before:

.navigationBarItems(leading:
NavigationLink(
destination: MyView(),
label: {
Text("MyView")
}))

After:

NavigationLink(
destination: MyView(),
isActive: $isActive,
label: {
EmptyView()
})

.navigationBarItems(leading:
Button(
action: self.isActive = true,
label: {
Text("My View")
}))

@FetchRequest predicate not updating when UserDefaults value changes SwiftUI

I would update the fetch request with a new predicate in the Button action and that update should trigger the request to be executed again

struct ContentView: View {
@AppStorage("showAvailable") private var showAvailable : Bool = false

@FetchRequest(sortDescriptors: [],
predicate: NSPredicate(value: true)
animation: .default)
private var list: FetchedResults<Item>

var body: some View {
Button("Cancel") {
showAvailable.toggle()
updatePredicate()
}
.onAppear {
updatePredicate()
}
}

private func updatePredicate() {
if showAvailable {
list.nspredicate = NSPredicate(format: "parent == nil")
} else {
list.nspredicate = NSPredicate(value: true)
}
}
}

SwiftUI: trigger redraw when NSManagedObject (not part of fetch request) bool property changes?

Move it all into a sub-View and use @ObservedObject like this:

struct DealerSection : View {
@ObservedObject var dealer: Dealer // body will be called when dealer changes.

var body: some View {
Section(header: DealerSHView(dealer: dealer)) {

if dealer.isOpen {
ForEach(section) { channel in
....
}
} else {
....
}

}
}

Then in your parent View's body it's just:

ForEach(carsResult) { section in

if let dealer = section.first?.dealer {

DealerSection(dealer: dealer)

}

}

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

...
}

FetchedResults won't trigger and SwiftUI update but the context saves it successfully

If first code block is a part of ObservableObject, then it does not look that third block, view one, depends on it, and if there is no changes in dependencies, view is not updated.

Try this approach.

But if there are dependencies, which are just not provided then change order of save and publisher as

try context.save()
self.objectWillChange.send()


Related Topics



Leave a reply



Submit