Post

Replies

Boosts

Views

Activity

Reply to Using an existing Core Data database with SwiftUI Previews
What I'm doing in my example application right now, trying to learn the SwiftUI ropes, is add code to prefill the Core Data store, within AppDelegate after the app has launched - for the debug config.It seems like SwiftUI preview mirrors the core data files created in the simulator folder to a another folder.See this link for information on how to find this hidden folder.It also seems that sometimes, preview will get stuck and you might need to restart Xcode or/and close open the view source window to get this to work. The whole thing seems not 100% stable right now.Here's my preview code:struct TeaListView_Previews: PreviewProvider { static var previews: some View { let moc = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext return TeaListView().environment(\.managedObjectContext, moc) } }And here's what's in AppDelegate:class func prefillDB() { #if DEBUG let shouldPrefillDB: Bool = UserDefaulwts.Default.Linkage(UserDefaults.KeysDEV.prefillDB.rawValue, nil).get() ?? false if shouldPrefillDB == true { PreviewContent.prefillDB() } #endif }func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. AppDelegate.prefillDB() return true }
Jan ’20