How can I pre-populate data from a json into core data in SwiftUI 2.0 when the app is loaded only for the first time. In SwiftUI 1.0 I did it in the AppDelegate.swift
but there is no such file anymore.
Asked
Active
Viewed 337 times
0

simibac
- 7,672
- 3
- 36
- 48
-
There are quite a few parts to this question - decoding JSON, doing something when app first loads (use `UserDefaults` or similar to track this), and working with CoreData. You should probably focus the question on one thing, like just about CoreData with SwiftUI 2 app lifecycle – George Oct 24 '20 at 14:40
-
AppDelegate is still there - https://stackoverflow.com/questions/62538110/swiftui-app-life-cycle-ios14-where-to-put-appdelegate-code/62538373#62538373 – Asperi Oct 24 '20 at 14:47
1 Answers
-1
Add your own AppDelegate.swift
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
return true
}
}
Then in your @main
View
add.
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
for iOS or
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
}
}
and
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
for MacOS

lorem ipsum
- 21,175
- 5
- 24
- 48