对 SwiftUI FetchRequest 的更改不会触发视图刷新? [英] Changes to SwiftUI FetchRequest not triggering view refresh?

查看:17
本文介绍了对 SwiftUI FetchRequest 的更改不会触发视图刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 FetchRequest 包装器,并在视图中更改获取结果的数据.这在视图第一次加载时起作用,视图随着所做的每次更改而更新..

Using the FetchRequest wrapper, and changing the data of the fetched results inside the view. This works the first time the view loads, the view updates with every change made..

问题是,如果我离开视图并再次返回,获取请求数据的更改会停止刷新视图.查看更改的唯一方法是再次离开和返回.

The problem is if I navigate away from the view and back again, the changes to fetch request data stop refreshing the view. The only way to see the changes is to navigate away and back once again.

我正在打印获取请求,并且对数据的更改正在起作用,这解释了为什么如果我再次导航回去,视图会正确显示,但是当视图上的数据发生更改时,视图不会刷新.

I am printing the fetch request, and changes to the data are working, which explains why if I navigate away back again the view displays correctly, but the view is not refreshing when that data is changed on the view.

这是一个错误还是我做错了什么?

Is this a bug or am I doing something incorrectly?

我有一个显示所有期刊的图书馆视图,第一部分是一个允许添加新期刊的文本字段,第二部分是所有期刊的列表,可以选择它们旁边的复选标记.

I have a library view that displays all journals, in the first section is a text field that allows adding a new journal, in the second section a list of all journals that can be selected to toggle a checkmark next to them.

第一次添加新期刊并选择它们有效..很奇怪,如果我离开然后回来,它们就会停止工作.

Adding new journals and selecting them works the first time.. Very odd that they stop working if I navigate away and come back.


struct LibraryView: View {

    // MARK: - PROPERTIES
    @Environment(.managedObjectContext) var context


    @FetchRequest(
      //  entity: Journal.entity(),
        sortDescriptors: [NSSortDescriptor(keyPath: Journal.name, ascending: true)],
        animation: .default
    ) var journals: FetchedResults<Journal>


    @State private var name = ""


    // MARK: - INITIAL VIEW -
    var body: some View {

        Form {

            Section(header: Text("New Journal")) {
                HStack {

                    TextField("Name", text: $name, onCommit: {
                        let newJournal = Journal(context: self.context)
                        newJournal.name = self.name
                        try? self.context.save()
                        self.name = ""
                    })

                    Button(action: { print("ACTION") }) {
                        Image(systemName: "plus.circle.fill")
                    }
                }
            }

            Section(header: Text("Journals")) {
                ForEach(journals) { journal in
                    Button(action: {
                        journal.isSelected.toggle()
                        try? self.context.save()

                    }) {
                        HStack {
                            Text(journal.name)
                            Spacer()
                            if journal.isSelected {
                                Image(systemName: "circle.fill")
                            }
                        }
                    }
                }
            }

        }.navigationBarTitle("Library")
    }
}

期刊只是一个简单的核心数据模型:

The journal is just a simple core data model:


import Foundation
import CoreData

public class Journal: NSManagedObject, Identifiable {

    @NSManaged public var name: String
    @NSManaged public var isSelected: Bool

}

我从导航栏中带有导航链接的选项卡视图导航到库视图:

I navigate to the Library View from a Tab View with a navigation link in the navigation bar:


struct JournalView: View {

    // MARK: - PROPERTIES
    @State private var presentFiltersView = false

    // MARK: - INITIAL VIEW -
    var body: some View {
        NavigationView {
            Text("Journal")
                .navigationBarTitle("Journal")
                .navigationBarItems(
                    leading: NavigationLink(destination: LibraryView()) {
                        Text("Library")
                    },
                    trailing: Button(action: { self.presentFiltersView = true }) {
                        Text("Filters")
                    })
        }.sheet(isPresented: $presentFiltersView) { Text("Filters") }
    }
}

我尝试从导航栏项目中删除导航链接,并将其用作标准列表行导航链接,结果是相同的.

I have tried removed the navigation link from the nav bar item, and using it as a standard list row navigation link, results are the same.

推荐答案

我想我知道为什么会这样了.问题是我导航到视图的方式.在导航栏项目中有一个导航链接是原因.相反,我将导航链接自己移到外面,并用一个控制导航链接活动状态的按钮替换它.提取请求现在似乎按预期工作.

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.

之前:

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

之后:

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

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

这篇关于对 SwiftUI FetchRequest 的更改不会触发视图刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆