【问题标题】:FetchedResult views do not update after navigating away from view & backFetchedResult 视图在离开视图并返回后不会更新
【发布时间】:2019-11-27 00:30:37
【问题描述】:

当您离开它们并返回时,SwiftUI FetchedResult 视图无法更新。

我创建了一个简单的待办事项列表应用程序作为示例。此应用由 2 个实体组成:

  • 一个 TodoList,可以包含许多 TodoItem(s)
  • 一个TodoItem,属于一个TodoList

首先,这是我的核心数据模型:

对于实体,我在CodeGen 中使用Class Definition

在这个例子中我只使用了 4 个小视图。

TodoListView:

struct TodoListView: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(
        entity: TodoList.entity(),
        sortDescriptors: []
    ) var todoLists: FetchedResults<TodoList>
    @State var todoListAdd: Bool = false

    var body: some View {
        NavigationView {
            List {
                ForEach(todoLists, id: \.self) { todoList in
                    NavigationLink(destination: TodoItemView(todoList: todoList), label: {
                        Text(todoList.title ?? "")
                    })
                }
            }
            .navigationBarTitle("Todo Lists")
            .navigationBarItems(trailing:
                Button(action: {
                    self.todoListAdd.toggle()
                }, label: {
                    Text("Add")
                })
                .sheet(isPresented: $todoListAdd, content: {
                    TodoListAdd().environment(\.managedObjectContext, self.managedObjectContext)
                })
            )
        }
    }
}

这只是获取所有 TodoList(s) 并将它们吐出到一个列表中。导航栏中有一个按钮,可以添加新的待办事项列表。

TodoListAdd:

struct TodoListAdd: View {
    @Environment(\.presentationMode) var presentationMode
    @Environment(\.managedObjectContext) var managedObjectContext
    @State var todoListTitle: String = ""

    var body: some View {
        NavigationView {
            Form {
                TextField("Title", text: $todoListTitle)

                Button(action: {
                    self.saveTodoList()
                    self.presentationMode.wrappedValue.dismiss()
                }, label: {
                    Text("Save")
                })

                Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                }, label: {
                    Text("Cancel")
                })
            }
            .navigationBarTitle("Add Todo List")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }

    func saveTodoList() {
        let todoList = TodoList(context: managedObjectContext)
        todoList.title = todoListTitle

        do { try managedObjectContext.save() }
        catch { print(error) }
    }
}

这只是保存一个新的待办事项列表,然后关闭模式。

TodoItemView:

struct TodoItemView: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    var todoList: TodoList
    @FetchRequest var todoItems: FetchedResults<TodoItem>
    @State var todoItemAdd: Bool = false

    init(todoList: TodoList) {
        self.todoList = todoList
        self._todoItems = FetchRequest(
            entity: TodoItem.entity(),
            sortDescriptors: [],
            predicate: NSPredicate(format: "todoList == %@", todoList)
        )
    }

    var body: some View {
        List {
            ForEach(todoItems, id: \.self) { todoItem in
                Button(action: {
                    self.checkTodoItem(todoItem: todoItem)
                }, label: {
                    HStack {
                        Image(systemName: todoItem.checked ? "checkmark.circle" : "circle")
                        Text(todoItem.title ?? "")
                    }
                })
            }
        }
        .navigationBarTitle(todoList.title ?? "")
        .navigationBarItems(trailing:
            Button(action: {
                self.todoItemAdd.toggle()
            }, label: {
                Text("Add")
            })
            .sheet(isPresented: $todoItemAdd, content: {
                TodoItemAdd(todoList: self.todoList).environment(\.managedObjectContext, self.managedObjectContext)
            })
        )
    }

    func checkTodoItem(todoItem: TodoItem) {
        todoItem.checked = !todoItem.checked

        do { try managedObjectContext.save() }
        catch { print(error) }
    }
}

这个视图获取属于被点击的 TodoList 的所有 TodoItem(s)。这就是问题发生的地方。我不确定是不是因为我在这里使用了init(),但是有一个错误。当您第一次进入此视图时,您可以点击待办事项以“检查”它,并且更改会立即显示在视图中。但是,当您导航到不同 TodoList 的不同 TodoItemView 并返回时,点击时视图不再更新。复选标记图像未显示,您需要离开该视图,然后重新输入它才能真正显示所述更改。

TodoItemAdd:

struct TodoItemAdd: View {
    @Environment(\.presentationMode) var presentationMode
    @Environment(\.managedObjectContext) var managedObjectContext
    var todoList: TodoList
    @State var todoItemTitle: String = ""

    var body: some View {
        NavigationView {
            Form {
                TextField("Title", text: $todoItemTitle)

                Button(action: {
                    self.saveTodoItem()
                    self.presentationMode.wrappedValue.dismiss()
                }, label: {
                    Text("Save")
                })

                Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                }, label: {
                    Text("Cancel")
                })
            }
            .navigationBarTitle("Add Todo Item")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }

    func saveTodoItem() {
        let todoItem = TodoItem(context: managedObjectContext)
        todoItem.title = todoItemTitle
        todoItem.todoList = todoList

        do { try managedObjectContext.save() }
        catch { print(error) }
    }
}

这只是允许用户添加一个新的待办事项。

如上所述,当您离开并重新进入 TodoItemView 时,视图会自动停止更新。以下是此行为的记录:

https://i.imgur.com/q3ceNb1.mp4

我到底做错了什么?如果我不应该使用init(),因为导航链接中的视图在它们出现之前就已经初始化了,那么正确的实现是什么?

【问题讨论】:

    标签: ios swift xcode core-data swiftui


    【解决方案1】:

    在谷歌搜索问题的各种不同短语后找到了解决方案:https://stackoverflow.com/a/58381982/10688806

    您必须使用“惰性视图”。

    代码:

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

    用法:

    NavigationLink(destination: LazyView(TodoItemView(todoList: todoList)), label: {
        Text(todoList.title ?? "")
    })
    

    【讨论】:

      猜你喜欢
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 2011-07-16
      • 1970-01-01
      相关资源
      最近更新 更多