Changes to SwiftUI FetchRequest not triggering view refresh?

Have the same issue. NavigationLink immediately instantiates destination view. Once I changed it into lazy view evaluation it begins working for me. But it still looks like a workaround.

struct LazyView<Content: View>: View {
    let build: () -> Content
    init(_ build: @autoclosure @escaping () -> Content) {
        self.build = build
    }
    var body: Content {
        build()
    }
}

See: https://www.objc.io/blog/2019/07/02/lazy-loading/

NavigationLink(destination: LazyView(LibraryView()))


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")
}))

Tags:

Ios

Swift

Swiftui