我如何在Swiftui中观察到一系列的核心对象?

发布于 2025-01-27 01:45:48 字数 1456 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2025-02-03 01:45:48

我们使用 fetchrequest/code>)观察托管对象阵列。要观察相关对象的数组提供nspredicate,例如,观察作者使用的书籍数组:“ rution =%@”,作者。例如

private var fetchRequest: FetchRequest<Book>
private var books: FetchedResults<Book> {
    fetchRequest.wrappedValue
}

init(author: Author) {
    let sortDescriptors = [SortDescriptor(\Book.timestamp, order: sortAscending ? .forward : .reverse)]
    fetchRequest = FetchRequest(sortDescriptors: sortDescriptors, predicate: NSPredicate(format: "author = %@", author), animation: .default)

fetchrequestdynamicproperty struct,与普通结构相比,它具有一些特殊的能力。在Swiftui调用Body之前,它调用在所有动态属性上进行更新,并且在fetchrequest的情况下,它将读取托管对象上下文的<<<<<<<<<代码> @environment 。

使用核心数据时,我们可以生成nsmanagedObject子类或扩展名来添加其他功能,例如类:NSManagedObject。在模型编辑器中,从菜单中选择生成模型子类。将所有内容从BookModel移动到书籍扩展名中。这些托管对象确实符合observableObject,它允许您使用@obseverobject,它再次使view> view struct> struct valuct value type type type tye the the the视图模型对象是因为当它检测到更改时,body将被调用。

在Swiftui中,我们尽量不要将对象用于视图状态,因为SwiftUi对值类型的使用旨在消除典型的对象的错误,因此我建议删除globalViewModel类,而是使用SwiftUi功能使用SwiftUi @State@AppStorage使view结构值类型具有视图模型对象语义。

We use FetchRequest (or @FetchRequest) to observe managed object arrays. To observe the array of related objects supply a NSPredicate, e.g. to observe the array of books for an author use: "author = %@", author. E.g.

private var fetchRequest: FetchRequest<Book>
private var books: FetchedResults<Book> {
    fetchRequest.wrappedValue
}

init(author: Author) {
    let sortDescriptors = [SortDescriptor(\Book.timestamp, order: sortAscending ? .forward : .reverse)]
    fetchRequest = FetchRequest(sortDescriptors: sortDescriptors, predicate: NSPredicate(format: "author = %@", author), animation: .default)

FetchRequest is a DynamicProperty struct which gives it some special abilities compared to a normal struct. Before SwiftUI calls body it calls update on all the dynamic properties, and in the case of FetchRequest it reads the managed object context out of the @Environment.

When using Core Data we can generate an NSManagedObject subclass or extension to add additional functionality, e.g. class Book: NSManagedObject. In the model editor choose generate model subclass from the menu. Move everything from BookModel into the Book extension. These managed objects do conform to ObservableObject which allows you to use @ObservedObject which again makes the View struct value type behave like a view model object because when it detects a change, body will be called.

In SwiftUI we try not to use objects for view state because SwiftUI's use of value types is designed to eliminate the bugs typical of objects, so I would suggest removing the GlobalViewModel class and instead using SwiftUI features @State and @AppStorage which makes the View struct value type have view model object semantics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文