Where to import static JSON in SwiftUI?

I have a static JSON file that's used as a data structure for importing information into my app. The JSON file doesn't change - if the content of the JSON file ever needs to be updated I'm happy (at this time) to issue an update in a point release. The JSON decoding only needs to be done once with the decoded result stored in an Environment variable for future reference. While it shouldn't be too computationally intensive to re-parse the JSON there really isn't any need once it's been done once.


So my question is - in a SwiftUI app where should I run the JSON decoder? I've looked at AppDelegate.swift, SceneDelegate.swift and ContentView.swift but I'm not sure if any of these are the right place to run a once-off decoder. Any advice appreciated.

Answered by jonprescott in 400796022

If this truly is a singleton set of data, the AppDelegate would seem to be a good point. In your applicationDidLoad() method, you could set up a singleton object with the decoded data, and provide an AppDelegate method to retrieve the data for whatever components of your application need it.

Accepted Answer

If this truly is a singleton set of data, the AppDelegate would seem to be a good point. In your applicationDidLoad() method, you could set up a singleton object with the decoded data, and provide an AppDelegate method to retrieve the data for whatever components of your application need it.

Thanks! Appreciate the guidance.

Where to import static JSON in SwiftUI?
 
 
Q