使用核心数据时,我们可以生成nsmanagedObject子类或扩展名来添加其他功能,例如类:NSManagedObject。在模型编辑器中,从菜单中选择生成模型子类。将所有内容从BookModel移动到书籍扩展名中。这些托管对象确实符合observableObject,它允许您使用@obseverobject,它再次使view> view struct> struct valuct value type type type tye the the the视图模型对象是因为当它检测到更改时,body将被调用。
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.
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.
发布评论
评论(1)
我们使用
fetchrequest
/code>)观察托管对象阵列。要观察相关对象的数组提供
nspredicate
,例如,观察作者使用的书籍数组:“ rution =%@”,作者
。例如fetchrequest
是dynamicproperty
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 aNSPredicate
, e.g. to observe the array of books for an author use:"author = %@", author
. E.g.FetchRequest
is aDynamicProperty
struct which gives it some special abilities compared to a normal struct. Before SwiftUI callsbody
it callsupdate
on all the dynamic properties, and in the case ofFetchRequest
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 fromBookModel
into the Book extension. These managed objects do conform toObservableObject
which allows you to use@ObservedObject
which again makes theView
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 theView
struct value type have view model object semantics.