Problems with core data in SwiftUI

I have been working on a 3x3 Rubik's cube app for iOS, and I have a timer in it. Whenever I try to add a time and save the context in a preview, it gives an error.

func addTime(){
        print("Started")
        formatTime()
        print("Formatted")
        withAnimation(){
            print(formattedTime)
            let time = Time(context: viewContext)
            time.time = formattedTime
            print(time.time!)
            do{
                print("dostart")
                try viewContext.save()
                print("saved")
            }catch{
                let error = error as NSError
                print("An error occurred: \(error)")
                fatalError("An error occurred: \(error)")
            }
        }
    }

This is the function that adds the time and saves the context.(The prints were for debugging purposes) When this function runs while testing in the preview, the do/catch returns "An error occurred: Foundation._GenericObjCError.nilError" When I try to run this in the simulator, it gives this error in Persistence.swift:

If you need it, here is my entire ContentView.swift:

import SwiftUI
import CoreData
var scrambles = ["R' B' L U' B2 D B' D' L R B L2 D' B L' U L2 B2 L B2 R' F L2 B' F' U2 B F' L2 F2", "D' B' L B' F L' B2 U' R2 D' U' L' D' R' F L U2 F' R2 D' B2 L' R' B' L R' U F L B2", "L R B' D' L D' U L2 R' B2 L' D2 U2 R' D' U' L D F' L2 D2 F D2 L' D' U' B' F2 U' B'", "U L2 R D' B2 D2 R2 D2 L2 R U2 R' U L2 D' F' D R B' F2 L' F' L B2 F' D' L R' F2 D"," D' U F L' R' U2 R' D B2 F U' L2 D2 B' L2 B2 R' F' D' U B' L' R' B' F L' B' L R2 D", "D2 U' F2 D' U' B2 F' U' R D2 U R2 D2 R2 U L B F2 D2 U2 B F2 L U2 F' U2 R D' L F'", "F D' U B F2 U' F' L2 B2 F2 U B2 F2 D' B' F' L' R2 B D U' B2 F' L' R B' U2 R2 D2 F'", "B F U2 B2 L U B F L2 R F' L2 F' U2 R B U' B2 U2 B' U2 L R D' U2 B' D L R' U"]
var starts = ["Start", "Stop"]
struct ContentView: View {
    @State var start = false
    @State var startstop = "Start"
    @State var watch = stopwatch()
    @State var elapsedTime: Int = 0
    @State var timeToSave: Int = 0
    @State var minutes: Int = 0
    @State var seconds: Int = 0
    @State var milliseconds: Int = 0
    @State var formattedTime: String = ""
    
    @Environment(\.managedObjectContext) private var viewContext
    
    @FetchRequest(entity: Time.entity(), sortDescriptors: [])
    private var timecd: FetchedResults<Time>
    var body: some View {
        NavigationView{
            VStack{
                Text("CubeTimer")
                    .font(.largeTitle)
                    .fontWeight(.bold)
                    .padding(2)
                Text(scrambles.randomElement()!)
                    .multilineTextAlignment(.center)
                    .padding(.top, 125.0)
                    .padding(.leading, 15)
                    .padding(.trailing, 15)
                    .font(.title)
                    .fontWeight(.bold)
                if start{
                    watch
                        .padding(.top, 30)
                        .fontWeight(.bold)
                        .onAppear{
                            startstop = "Stop"
                            watchToggle()
                        }
                    var timer: Timer {
                        Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) {_ in
                            elapsedTime += 1
                            //print("\(minutes).\(seconds)")
                            print("called")
                            print(elapsedTime)
                            //figure out how to stop timer and save time to core data
                        }
                    }
                }
                
                Button(action: {
                    start.toggle()
                    if start == false{
                        timeToSave = elapsedTime
                        print(timeToSave)
                        print(elapsedTime)
                        watchToggle()
                        buttonToggle()
                        addTime()
                    }
                    //print(start)
                }){
                    Text(startstop)
                }.padding(.top, 7)
                    .font(.system(size: 30))
                Spacer()
                //figure out how to stop timer and save time to core data
            }
            List{
                ForEach(timecd){time in
                    Text(time.time ?? "Not found")
                }
            }
        }
    }
    func buttonToggle(){
        if start{
            startstop = "Stop"
        }else{
            startstop = "Start"
        }
    }
    func watchToggle(){
        print("runs")
        watch.toggleRun()
        watch.testForIsRunning()
        //print(watch.isRunning)
    }
    func formatTime(){
        print("Formatting")
        print(timeToSave)
        minutes = timeToSave / 6000
        seconds = (timeToSave % 6000) / 100
        milliseconds = timeToSave % 100
        print(minutes)
        print(seconds)
        print(milliseconds)
        formattedTime = "\(minutes):\(seconds).\(milliseconds)"
    }
    func addTime(){
        print("Started")
        formatTime()
        print("Formatted")
        withAnimation(){
            print(formattedTime)
            let time = Time(context: viewContext)
            time.time = formattedTime
            print(time.time!)
            do{
                print("dostart")
                try viewContext.save()
                print("saved")
            }catch{
                let error = error as NSError
                print("An error occurred: \(error)")
                fatalError("An error occurred: \(error)")
            }
        }
    }

    func buttontext(){
        if start{
            startstop = "Stop"
        }else{
            startstop = "Start"
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Could somebody please help me resolve this issue?

Problems with core data in SwiftUI
 
 
Q