fatbobman / blogComments

1 stars 0 forks source link

深度解读 Observation —— SwiftUI 性能提升的新途径 | 肘子的Swift记事本 #195

Open fatbobman opened 1 year ago

fatbobman commented 1 year ago

https://www.fatbobman.com/posts/mastering-Observation/

在 WWDC 2023 中,苹果介绍了 Swift 标准库中的新成员:Observation 框架。它的出现有望缓解开发者长期面临的 SwiftUI 视图无效更新问题。本文将采取问答的方式,全面而详尽地探讨 Observation 框架,内容涉及其产生原因、使用方法、工作原理以及注意事项等。

skywalkerlw commented 1 year ago

在“测试一:” 中,为啥store.a = 100不会引起onChange?

fatbobman commented 1 year ago

在“测试一:” 中,为啥store.a = 100不会引起onChange?

因为在执行 store.b = 100 时,便已经调用了 onChange 闭包。该闭包只会调用一次

moekyo commented 10 months ago

Macro应该是只能增加代码,而不能对源代码做修改或者删除的吧?Macros 说明

fatbobman commented 10 months ago

Macro应该是只能增加代码,而不能对源代码做修改或者删除的吧?Macros 说明

是的,这个部分出现了错误。谢谢

mcmay commented 8 months ago

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.

fatbobman commented 8 months ago

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.

mcmay commented 8 months ago

@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.

Shinolr commented 8 months ago

比如,当前开发者通常会使用结构体( Struct )来构建应用的状态模型。使用了 Observation 框架后,为了实现属性级别的观察,我们应该改用 Observation 框架创建可观察对象,甚至多层嵌套的对可观察对象来构建状态模型。

苹果一直推荐使用struct作为model的类型。但是越来越多的官方sample code使用class构建model,最近几年尝试wwdc推出的新东西时,总是不知道该用struct还是class。看完文章,终于解开疑惑,知道如何选型了,谢肘大。

guoyingtao commented 6 months ago

发现一个typo @Publsiehd

@StateObject var store = Store() // 只要有属性( @Publsiehd )发生变化,便对 body 重新评估

非常棒的文章 👍🏻 多谢分享!

fatbobman commented 6 months ago

发现一个typo @Publsiehd

@guoyingtao 谢谢,已修改

opserx commented 3 months ago

我觉得有种场景可能很常见。一个模型我需要使用SwiftData存储,同时我也需要也他是@Obsevable。这种情况怎么做?需要定义两个类么?

fatbobman commented 3 months ago

@opserx 没明白。SwiftData 的 Model 本身就是使用了 Observation ,如果你的意思是,希望其中的部分属性基于 Observation 框架,但是不想持久化,那么就用 @Transient 来标注它。尽管这样不是最推荐的,但也基本上够用了。

opserx commented 3 months ago

谢谢。是我糊涂,没想道SwiftData 的 Model 本身就是使用了 Observation。这么简单居然不知道。

2024年6月12日 16:16,东坡肘子 @.***> 写道:

@opserx https://github.com/opserx 没明白。SwiftData 的 Model 本身就是使用了 Observation ,如果你的意思是,希望其中的部分属性基于 Observation 框架,但是不想持久化,那么就用 @transient https://github.com/transient 来标注它。尽管这样不是最推荐的,但也基本上够用了。

— Reply to this email directly, view it on GitHub https://github.com/fatbobman/blogComments/issues/195#issuecomment-2162388982, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEZXMGJ2OU5WLZX3I7GOMV3ZG77UBAVCNFSM6AAAAAAZLFKL2SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNRSGM4DQOJYGI. You are receiving this because you were mentioned.