Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI with Core Data getting Blank Screen in simulator in Xcode 12

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?

like image 545
Sai Durga Mahesh Avatar asked Sep 22 '20 07:09

Sai Durga Mahesh


People also ask

How to create an app with SwiftUI in Xcode 12?

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.

Why can't I use core data with the SwiftUI life cycle?

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.

How do I add Core Data to a SwiftUI project?

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.

How to persist data in an app with SwiftUI?

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.


3 Answers

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)
    }
}
like image 61
uplearned.com Avatar answered Jan 04 '23 17:01

uplearned.com


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.

like image 20
L-vis Avatar answered Jan 04 '23 15:01

L-vis


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.

like image 41
Daniel Price Avatar answered Jan 04 '23 16:01

Daniel Price