Showmax / ios-swiftui-redraws

Experimenting with view redraws in SwiftUI
17 stars 4 forks source link

In solution #3, how do you report changes back to the parent observable object? #1

Closed 0ranga closed 1 year ago

0ranga commented 1 year ago

First of all, thank you for this article, it is very interesting and informative! I happened to stumble upon the same kind of problems while developing an iOS app.

I do have one question, in solution #3 you mentioned:

And, if necessary, you can report changes back to the parent observable object.

Can you explain how you would do that please?

peruginni commented 1 year ago

Hello, glad to hear that article was handy. Thanks for the question!

First I'd like to highlight that on WWDC 2023 Apple addressed these redraws using @Observable macro. So our suggestions (like splitting to multiple objects) might not even be needed in most cases. Now SwiftUI will redraw only those parts of UI for whose the related model property actually did change, and does not redraw unrelated UI. I didn't tried it yet but seems that it might be backward compatible to older iOS versions, since Swift 5.9 macros are generating helper code that is compiled with app, so just depends if it generates something that is able work within SwiftUI on older iOS versions. I will post here if I found out more details. For more information see: https://developer.apple.com/wwdc23/10149

But if you need to report changes back to parent, I've added examples of few options:

1) Example3b.swift via closure in child object, parent will use it for listening

2) Example3c.swift via property publishers (for specific changes) or objectWillChange (for all changes)

3) Example3d.swift via common external manager

I also noticed this is nicely handled in TCA architecture (but then all your components have to use this architecture).

0ranga commented 1 year ago

Thanks a lot for taking the time to answer in such details!

The info you provided is really helpful. Indeed, the new changes announced at the WWDC are quite exciting, I'm only concerned that it might not be backward-compatible. Same as you, I haven't had the time to inform myself about that yet so your comments are still very useful ;)