Lazy View SwiftUI
I came up with this solution independently while working on a project. After some more Googling, I found others had come across the same bug and solved it. I am using this as an opportunity to try my first CocoaPod and SwiftPackage.
Why Lazy Load a SwiftUI View?
The primary problem is with NavigationLink
. The destination view given will be immediately instantiated and loaded--before the user even navigates to the details page. This is a problem for large details pages that you don't want to have immediately loaded with the previous screen.
Example project:
To run the example project, clone the repo, and run pod install
from the Example directory first.
Example usage:
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: LazyView(Text("My details page"))) {
Text("Go to details")
}
}
}
}
Installation
CocoaPods
LazyViewSwiftUI is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'LazyViewSwiftUI'
SPM
LazyViewSwiftUI is also available through Swift Package Manager.
Once you have your Swift package set up, adding LazyViewSwiftUI as a dependency is as easy as adding it to the dependencies value of your Package.swift.
dependencies: [ .package(url: "https://github.com/joehinkle11/Lazy-View-SwiftUI") ]