我正在尝试将我的 @fetchrequest 属性移动到辅助类,这不是View
,但每次我尝试这样做时,都会遇到严重的指令错误。
谁能帮我?
这是我的代码示例:
视图模型:
class ViewModel {
@FetchRequest(entity: Teste.entity(), sortDescriptors: []) var teste: FetchedResults<Teste>
}
View:
struct ContentView: View {
let viewModel: ViewModel
init(viewModel: ViewModel) {
self.viewModel = viewModel
}
var body: some View {
List(viewModel.teste) { item in // error happens in this line
Text(item.id ?? "")
}
}
}
谢谢你!
我在尝试弄清楚如何自己实现类似的结果时遇到了你的问题。我猜你现在可能已经解决了这个问题,但为了那些遇到同样问题的人的利益,我将尝试回答,或者至少指出可能在哪里找到答案:-)
正如已经提到的@FetchRequest
属性包装器在视图组件之外不起作用(这似乎有点奇怪的功能遗漏)
我发现的最好的替代方案是使用Combine
发布者的更新NSFetchedResultsController
正如建议的阿波斯托洛斯 https://stackoverflow.com/users/928870/apostolos在他的回答中here https://stackoverflow.com/questions/60495494/how-to-using-combine-to-react-to-coredata-changes-in-the-background/60535155#60535155并在链接中详细说明中柱 https://medium.com/better-programming/combine-publishers-and-core-data-424b68fe9473
我整理了一个简单的概念验证示例,用于测试和演示 iOS14 及其新生命周期管理的方法。这个演示应用程序可以在我的 GitHub 存储库中找到here https://github.com/shufflingB/swiftu-core-data-with-model
祝你好运。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)