ios - FetchedResult 视图在离开视图并返回后不会更新
问题描述
当您离开它们并返回时,SwiftUI FetchedResult 视图无法更新。
我创建了一个简单的待办事项列表应用程序作为示例。这个应用程序由 2 个实体组成:
- 一个 TodoList,可以包含许多 TodoItem(s)
- 一个 TodoItem,属于一个 TodoList
首先,这是我的核心数据模型:
对于实体,我使用Class Definition
in CodeGen
。
我在这个例子中只使用了 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()
,因为导航链接中的视图在它们出现之前就被初始化了,那么正确的实现是什么?
解决方案
经过数小时谷歌搜索该问题的各种不同短语后找到了解决方案: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 ?? "")
})
推荐阅读
- reactjs - 如何在 socket.io 事件上更新反应组件
- lex - 如何解决我无法匹配我在 Lex 中定义的模式的问题?
- python - 如何使用python从excel表中获取函数值?
- php - 我需要克服对我的 ISP 的 PHP 环境的限制
- mysql - 如何从表单更新mysql表行:Laravel 5.8
- mysql - 没有 dll 就无法运行需要 mysql.data.dll 的程序
- mysql - 尝试从 Moodle 3.6 的问题库中添加随机问题时弹出编码错误
- python - 将 txt 文件与 csv 文件与 python 进行比较
- javascript - Promise 数组返回空
- asp.net-mvc-5 - 解析器错误消息:无法加载类型“MvcInterop.InteropMasterPage”