Swift UI runtime order

Hello
I am trying to learn Swift UI and while following the tutorial here : https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation, I noticed that we make a function called load, and call it globally to populate the locations array.

My general question is what is the order of runtime of files in Swift UI (what gets called when, right from where you launch the app), and more specifically, when does load get called in that sequence.

Thank you!

P.S. : I am not sure if this is an appropriate place to ask this question, so sorry if its not!
Accepted Answer
The trailing comma is making the URL hard to follow.

Building Lists and Navigation

Generally, it is very hard to specify when some executable statements in a SwiftUI app will be executed.
One reason is that many parts of SwiftUI are not clearly documented, another is many parts will be executed on demand.

when does load get called in that sequence.

So, focus on this most specific case.

In this case, load(_:) is used as the initial value in the definition of global variable landmarks.
Global variables are lazily initialized in Swift, meaning at the first place it is accessed.

So, you may need to find where it is accessed first.

You can find some places using landmarks, but I think it is line 10 of LandmarkList.swift:
Code Block
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
A view showing a list of landmarks.
*/
import SwiftUI
struct LandmarkList: View {
var body: some View {
NavigationView {
List(landmarks) { landmark in //<- The first usage of `landmarks`
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
.navigationTitle("Landmarks")
}
}
}


So, when the body of LandmarkList is evaluated first, landmarks is accessed and load(_:) is called.

Is this sufficient for you?
Whether your answer is yes or no, we cannot go deeper than this, as SwiftUI does not specify when body is evaluated.
One thing sure is that body is evaluated before the view is shown.
Thus, load(_:) is called sometime before LandmarkList is shown. That's all we can say about when.

Yes this is sufficient, tysm!
Swift UI runtime order
 
 
Q