I'm refactoring my old iPhone app that uses Storyboards and AppDelegate to use SwiftData.
When I launch the app, I hit the issue because my ModelContainer in my "quick sanity check integration" was being created after static variables containing "default recipes".
I had to use the custom storyboard initializer to work around this issue, until I revamp the launch screen from a UIViewController backed by a storyboard to a SwiftUI view and the new app structure.
This uses the instantiateViewController(identifier:creator:) variant of the Storyboard class so that I can inject the dependencies for SwiftData.
https://developer.apple.com/documentation/uikit/uistoryboard/3213989-instantiateviewcontroller
On start I have this logic in application(didFinishLaunchingWithOptions:
let dataContainer = DataContainer(enableCloudKit: false, isStoredInMemoryOnly: false)
let userSettings = UserSettings()
let recipeHelper = RecipeHelper(userSettings: userSettings)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let measureVC = storyboard.instantiateViewController(identifier: "MeasureViewController") { coder in
return MeasureViewController(coder: coder, userSettings: userSettings, recipeHelper: recipeHelper, dataContainer: dataContainer)
}
The offending code was a SwiftData static variable.
static let harioV60_size1 = CoffeeRecipe(
dateCreated: .now,
dateModified: .now,
dateBrewed: nil,
title: "Hario V60",
brewMethod: .harioV60_Size1,
defaultRecipe: true,
waterToCoffeeRatio: 16.2,
coffeeWeight: 21,
waterWeight: 340,
bloomRatio: 2,
bloomTime: 30,
pourTime: 2.5 * 60,
drainTime: 3 * 60
)
Topic:
Developer Tools & Services
SubTopic:
Xcode
Tags: