Open fatbobman opened 1 year ago
在“测试一:” 中,为啥store.a = 100不会引起onChange?
在“测试一:” 中,为啥store.a = 100不会引起onChange?
因为在执行 store.b = 100 时,便已经调用了 onChange 闭包。该闭包只会调用一次
When SwiftData comes into the scene to play, things will change. SwiftData seems to add the observation by default via the @Model macro so the @Observation macro becomes superfluous.
When SwiftData comes into the scene to play, things will change. SwiftData seems to add the observation by default via the @model macro so the @observation macro becomes superfluous.
Yes, @Model
covers the functionalities implemented by the @Observable
macro. In the article Unveiling the Data Modeling Principles of SwiftData, I described the specific implementation details.
@fatbobman
When SwiftData comes into the scene to play, things will change. SwiftData seems to add the observation by default via the @model macro so the @observation macro becomes superfluous.
Yes,
@Model
covers the functionalities implemented by the@Observable
macro. In the article Unveiling the Data Modeling Principles of SwiftData, I described the specific implementation details.
Thanks for sharing. I've read that article before this one. It's an in-depth treatment of the modelling mechanism of SwiftData. By the way, the question about the runtime exception of my app project raised after that article has now been resolved. It turned out to be the misplacement of a .sheet() modifier in a wrong place in my code, although I'm still at a loss why the exception happened because that. There is surely a lot to be explored in the interplay between SwiftUI and SwiftData.
比如,当前开发者通常会使用结构体( Struct )来构建应用的状态模型。使用了 Observation 框架后,为了实现属性级别的观察,我们应该改用 Observation 框架创建可观察对象,甚至多层嵌套的对可观察对象来构建状态模型。
苹果一直推荐使用struct
作为model的类型。但是越来越多的官方sample code使用class
构建model,最近几年尝试wwdc推出的新东西时,总是不知道该用struct
还是class
。看完文章,终于解开疑惑,知道如何选型了,谢肘大。
发现一个typo @Publsiehd
@StateObject var store = Store() // 只要有属性( @Publsiehd )发生变化,便对 body 重新评估
非常棒的文章 👍🏻 多谢分享!
发现一个typo
@Publsiehd
@guoyingtao 谢谢,已修改
https://www.fatbobman.com/posts/mastering-Observation/
在 WWDC 2023 中,苹果介绍了 Swift 标准库中的新成员:Observation 框架。它的出现有望缓解开发者长期面临的 SwiftUI 视图无效更新问题。本文将采取问答的方式,全面而详尽地探讨 Observation 框架,内容涉及其产生原因、使用方法、工作原理以及注意事项等。