Skip to main content

2 posts tagged with "learning"

View All Tags

· One min read

Reference: Demystify SwiftUI WWDC21

When SwiftUI looks at your code, it will look for three things:

  • Identity
  • Lifetime
  • Dependencies

Identity

  • Explicit: In this case, .dogTagID represents the ID of the rescueDog data type which conforms to the Identifiable protocol.

    • With a stable ID, SwiftUI can optimize performance. cleanshot-2024-03-18-000001.jpg
  • Implicit or Structural Identity: If a statement or switch statement in a SwiftUI view's body (@ViewBuilder) creates an implicit ID based on structural differences (i.e., the position of the view relative to its hierarchy).

cleanshot-2024-03-18-000004@2x.jpg

  • Avoid using AnyView if possible.

cleanshot-2024-03-18-000005@2x.jpg

View Lifetime

  • State Lifetime == View Lifetime, meaning when the view's identity changes, the state is destroyed along with that identity.

cleanshot-2024-03-18-000007@2x.jpg

cleanshot-2024-03-18-000008@2x.jpg

With this implementation, when date < .now, a different view modifier identity is created, which is not stable.

cleanshot-2024-03-18-000009@2x.jpg

A better version would be to tie the condition in the value of opacity, so there is just a single View Modifier, which means better performance, more correct code, and SwiftUI is optimized for this kind of code.

cleanshot-2024-03-18-000010@2x.jpg

· 2 min read

#learning Data Essential in SwiftUI - 2020

  • @State create a new source of truth
  • @Binding create a reference to the source of truth allow write access to that state

State-Binding

Performance consideration: avoid Repeated Heap allocation problem

CleanShot 2024-03-18 at 08.54.28@2x.png|400

CleanShot 2024-03-18 at 08.54.47@2x.png|400

  • @ObservableObject allow multiple SwiftUI view to subscribe to its changes (specifically before the property will change)
  • ObservableObject protocol is for class (reference type only), which mean it will be kept alive in the memory if there is reference to it
  • Class follow this protocol will usually need to have @Published property that will trigger event that ties to SwiftUI view updates
  • ObservableObject can be used to provide a single source of truth through view / app hierarchy
  • @Published can be used to mark property can be observed or bound to (Binding)

CleanShot 2024-03-18 at 10.09.25.jpg

  • ObservableObject can be used to provide a single source of truth through view / app hierarchy, either as a single Instance (one object) or multiple instances CleanShot 2024-03-18 at 10.19.33.jpg

There is 3 way that View can subscribe to changes of @ObservableObjects

  • @ObservedObject, the life cycle of the observed object is not managed by view life cycle, but somewhere else
  • @StateObject , the object is only initiated just before body of the view is created, and destroyed when the view is no longer rendered. In short, life-cycle of the object is managed by the view
  • @EnvironmentObject is usually used when we want to create a globally shared Source of truth

Data Life-cycle

  • Tied to the way we defined it

CleanShot 2024-03-18 at 10.50.31.jpg

  • Scene is new screen or new window instance, think Split view in ipad, windows in macos