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. Demystifying
    SwiftUI previews
    Marina Gornostaeva | @hybridcattt

    View Slide

  2. Agenda
    ! Other use-cases
    " Preview lifecycle
    # The build process
    $ How live reloading works
    ⏸ Reduce previews pausing

    View Slide

  3. ! Other use-cases

    View Slide

  4. ! Other use-cases

    View Slide

  5. " Lifecycle of a SwiftUI preview
    • Build once
    • Live reload on changes
    • Pause if change is unsupported by live reloading
    • Repeat

    View Slide

  6. # Build process
    • For DEBUG
    • Detect by ENABLE_PREVIEWS build setting
    • Use ProcessInfo in runtime

    View Slide

  7. $ 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")
    }
    }

    View Slide

  8. $ 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

    View Slide

  9. ⏸ Improving live reloading
    • Use computed vars
    • Check build scripts
    • Auto-resume previews

    View Slide

  10. Thanks &
    @hybridcattt
    https://hybridcattt.com/blog/ ixing-swiftui-previews

    View Slide