Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Demystifying SwiftUI Previews

Demystifying SwiftUI Previews

Understand how SwiftUI previews work, why they pause, and how to fix it.

Marina Vatmakhter

April 08, 2022
Tweet

More Decks by Marina Vatmakhter

Other Decks in Technology

Transcript

  1. Agenda ! Other use-cases " Preview lifecycle # The build

    process $ How live reloading works ⏸ Reduce previews pausing
  2. " Lifecycle of a SwiftUI preview • Build once •

    Live reload on changes • Pause if change is unsupported by live reloading • Repeat
  3. # Build process • For DEBUG • Detect by ENABLE_PREVIEWS

    build setting • Use ProcessInfo in runtime
  4. $ Dynamic replacement • Swift feature called @_dynamicReplacement • Pitched

    in 2018 • Supports changes in implementation of a function, computed variable, initializer or subscript struct MyStruct { dynamic func x() { print("x") } } extension MyStruct { @_dynamicReplacement(for: x()) func y() { print("y - replaced dynamically") } }
  5. $ Limitations of live reloading • It’s applied to everything

    in the current ile • Supported: changes to bodies of functions, properties, initializers • Not supported: • Editing comments • Signature changes • Adding/removing types, functions, properties • Initial value changes • Changes in other iles
  6. ⏸ Improving live reloading • Use computed vars • Check

    build scripts • Auto-resume previews