In XCode 12, if I create a new SwiftUI App and check the "Use Core Data" button, the resulting application (with no changes) shows a blank screen in simulator (as well as on a device). In preview it shows the example timestamps as expected. Why are the simulator/device not showing the example timestamps?
To get started, open Xcode 12 and create a new “App” under “Multiplatform” or “iOS”. You can name your project however you want, for instance, “PizzaRestaurant”. But make sure to use SwiftUI as the “Interface” mode and SwiftUI App as the “Life Cycle” mode. Also, make sure that you check the “Use Core Data” box.
If the Life Cycle is set to SwiftUI App, the Core Data checkbox is disabled. I'm assuming either it's not ready yet or it won't be enabled for some specific reason. Xcode 12 does not currently include a project template that demonstrates how to use Core Data with the SwiftUI Life Cycle option.
Adding Core Data to a SwiftUI project just takes two small steps: To add a new Core Data model file to your project select File -> New -> File (cmd + N) and select Data Model from the Core Data section in the file type picker. After selecting this, pick a name for your model.
In this Core Data with SwiftUI tutorial, you’ll learn to persist data in an app using @State, @Environment and @FetchRequest property wrappers. Imagine jotting down something important in Notes, only to find your data is gone the next time you open the app! Fortunately, persistence is in excellent hands on iOS.
The toolbar items default code is broken in SwiftUI: Use this in the template code. Embed the List into a NavigationView and then The buttons in a HStack.
var body: some View {
NavigationView { //added
List {
ForEach(items) { item in
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
}
.onDelete(perform: deleteItems)
} .toolbar {
#if os(iOS)
HStack { //added
EditButton()
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}//added
#endif
}
}//added NavView embed
}
Also to get the preview to work you need to change the PersistenceController to shared not preview.
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.shared.container.viewContext)
}
}
If you want to see the sample inputs from the template (10 rows with timestamp) in your simulator, you need to change in App.swift:
let persistenceController = PersistenceController.shared
to
let persistenceController = PersistenceController.preview
Without this change, the template provided by Apple shows the sample input only in the canvas preview of ContentView. The Persistence.swift file has two static variables: shared and preview. The .shared one is just initiating an (empty) PersistenceController while the .preview static variable initiates a PersistenceController, adds ten items with the current time stamp to the viewContext and saves it.
Clearing the data in the simulator did not work for me.
I'm struggling with .toolbar but find it only works with a NavigationView in the released XCode 12.
So if you're using the template that comes when you click to use Core Data, just add to the ContentView.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With