FB18559986: Suggestion to improve foundation models sample project
Submission Date
2025-07-02
Status
Open
Area
Documentation
Operating System Version
iOS 26
Type
Suggestion
Description
Sample Code https://developer.apple.com/documentation/foundationmodels/adding-intelligent-app-features-with-generative-models
Hi I'd like to suggest a small improvement to this sample project, in FoundationModelsTripPlannerApp.swift instead of:
struct FoundationModelsTripPlannerApp: App {
private var modelData = ModelData.shared
var body: some Scene {
WindowGroup {
LandmarksView()
.environment(modelData)
}
}
}
It is preferable to delay loading the model until body is called. So it won't be called during Previews which need all the help they can get to launch quicker. E.g.
struct FoundationModelsTripPlannerApp: App {
var body: some Scene {
WindowGroup {
LandmarksView()
.environment(ModelData.shared)
}
}
}
In Previews although the app struct is init and thus all of its properties are init too but the body is not called so expensive model object init should be delayed until body.
I'm trying to submit edits like this to all the samples so they all can be consistent and help developers to get it right. Thanks
Keywords
sample code
Prerequisites
- [x] The title follows the format
FB<number>: <title> - [x] I will keep this issue updated with Apple's responses