swiftui - 在 SwiftUI NavigationLink 视图中删除领域对象
问题描述
我有一个列表视图、行视图(对于列表中的每个元素)和详细视图(将由行视图中的 NavigationLink 打开)。在详细视图中,我有一个删除按钮,我试图删除领域对象并返回列表视图,但我在 AppDelegate 顶部收到以下错误:
由于未捕获的异常“RLMException”而终止应用程序,原因:“对象已被删除或无效。”
详细视图(简单):
struct TransactionDetailView: View {
let realm = try! Realm()
`Realm Objects which contains some information (title, value ...). Can be null, if the user creates a new one`
@State var transaction: MyTransaction?
@Environment(\.presentationMode) var presentationMode
var body: some View {...}
.navigationBarItems(trailing:
Button(action: {
if(self.transaction != nil) {
try! realm.write {
realm.delete(self.transaction!)
}
}
`If i remove this line and go manual back to the list view i have no issues`
self.presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "trash")
})
}
列表和行视图(简单):
struct TransactionsView: View {
//get the DB
let realm = try! Realm()
//get the observable transactions
@ObservedObject var transactions = BindableResults(results: try! Realm().objects(MyTransaction.self))
var body: some View {
...
List {
ForEach(transactions) { transaction in
TransactionRow(transaction: transaction, ...)
}
}
}
}
struct TransactionRow: View {
var transaction: MyTransaction
...
var body: some View {
NavigationLink(destination: TransactionDetailView(transaction: transaction)){...}
...
}
}
我看不出我的问题为什么它不起作用,但是当我只删除对象并关闭详细视图手册时,一切都会好起来的。
谢谢你的帮助
解决方案
推荐阅读
- windows - Jboss 7.2 - [org.jboss.config] file.encoding - Jboss 作为 Windows 服务
- javascript - 在反应中刷新后路由未定义
- python - os.path.abspath 忽略最后一个目录
- c++ - nullptr 访问类方法 VS 静态方法
- excel - 允许网络上的其他人在我的本地 PC 上打开工作簿
- sql - 无法在更新 postgres 中使用除外
- ansible - 如果在 Ansible 中可以访问第一台主机,则跳过其他主机
- angular - 如何将带有一些 id 的路由重定向到人类友好的 url 并在角度路由中处理规范 url?
- python - 当系统中没有安装 python3 时,我可以只在虚拟环境中使用它吗?
- html - 使用chrome从网站中提取表格数据