Xcode 15 - Preview Crashes with error log but works when parent view is Pinned

Hello

I am in need of help to try and understand what is going on with XCode and Previews when using Swift Data.

I have the following path:

View 1 (Calandar Screen) -> Passes Model -> View 2 (Details Screen)

I have a situation where when working on View 2 it will not run in Preview mode when the Pin is not selected. However if I pin View 1 and then go to work on View 2 preview works fine.

Now I am guessing that this might have something to do with Swift Data and by having View 1 open it generates the Model to be passed but I might be wrong?

I am also struggling to understand and read the error log that excode generates.

I have Preview setup in View 2 as follows

#Preview {
    DiaryReadView(diaryModel: DiaryModel())
        .environmentObject(NavigationStateManager())
        .modelContainer(for: [DiaryModel.self], inMemory: true)
}

Error Log


-------------------------------------
Translated Report (Full Report Below)
-------------------------------------

Incident Identifier: 5C8FAD15-959E-4E0A-AFF4-55A739D54110
CrashReporter Key:   CC891733-97EE-34E0-EDB7-394B181A2623
Hardware Model:      iMac20,1
Process:             Diary [36381]
Path:                /Users/USER/Library/Developer/Xcode/UserData/Previews/Simulator Devices/78258A6C-0145-4E82-ACCD-1F0539F2148A/data/Containers/Bundle/Application/970BD3F5-925F-47BD-A214-7F7B0226A04F/Diary.app/Diary
Identifier:          OR-1
Version:             1.0 (1)
Code Type:           X86-64 (Native)
Role:                Foreground
Parent Process:      launchd_sim [29471]
Coalition:           com.apple.CoreSimulator.SimDevice.78258A6C-0145-4E82-ACCD-1F0539F2148A [78195]
Responsible Process: SimulatorTrampoline [48509]

Date/Time:           2023-10-24 10:55:15.3444 +0100
Launch Time:         2023-10-24 10:55:14.7909 +0100
OS Version:          macOS 14.0 (23A344)
Release Type:        User
Report Version:      104

Exception Type:  EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes: 0x0000000000000001, 0x0000000000000000
Termination Reason: SIGNAL 4 Illegal instruction: 4
Terminating Process: exc handler [36381]

Triggered by Thread:  0

Thread 0 Crashed:
0   libswiftCore.dylib            	    0x7ff814aa36f2 0x7ff814a76000 + 186098
1   SwiftData                     	    0x7ff84952f7dd 0x7ff849514000 + 112605
2   SwiftData                     	    0x7ff8495b1746 0x7ff849514000 + 644934
3   SwiftData                     	    0x7ff8495b64c9 0x7ff849514000 + 664777
4   SwiftData                     	    0x7ff84956f726 0x7ff849514000 + 374566
5   Diary                         	       0x10a4e9635 DiaryModel.init() + 357 (@__swiftmacro_5Diary0A5Model0B0fMm_.swift:2)
6   Diary                         	       0x10a4e94c2 DiaryModel.__allocating_init() + 34
7   DiaryReadView.1.preview-thunk.dylib	       0x10bf15533 closure #1 in static $s025Diary_PreviewReplacement_A10ReadView_133_E76E9AE78936AC4C16C3EA33E995B161Ll0B0fMf_15PreviewRegistryfMu_.makePreview() + 51 (@__swiftmacro_025Diary_PreviewReplacement_A10ReadView_133_E76E9AE78936AC4C16C3EA33E995B161Ll0B0fMf_.swift:9)
8   PreviewsInjection             	       0x10ac7f386 0x10ac41000 + 254854
9   PreviewsInjection             	       0x10ac8025d 0x10ac41000 + 258653
10  libswift_Concurrency.dylib    	    0x7ff85b04c76c 0x7ff85b03b000 + 71532
11  PreviewsInjection             	       0x10ac7f0e5 0x10ac41000 + 254181
12  PreviewsInjection             	       0x10ac83108 0x10ac41000 + 270600
13  PreviewsInjection             	       0x10ac83c1c 0x10ac41000 + 273436
14  PreviewsInjection             	       0x10ac70a0d 0x10ac41000 + 195085
15  PreviewsInjection             	       0x10ac7387a 0x10ac41000 + 206970
16  PreviewsInjection             	       0x10ac5a630 0x10ac41000 + 103984
17  PreviewsInjection             	       0x10ac5aaf4 0x10ac41000 + 105204
18  PreviewsInjection             	       0x10ac753f0 0x10ac41000 + 214000
19  PreviewsInjection             	       0x10ac493f3 0x10ac41000 + 33779
20  PreviewsInjection             	       0x10ac48a85 0x10ac41000 + 31365
21  PreviewsFoundation            	       0x10b741df9 0x10b6a1000 + 658937
22  libdispatch.dylib             	    0x7ff800156a90 0x7ff800155000 + 6800
23  libdispatch.dylib             	    0x7ff800157d3a 0x7ff800155000 + 11578
24  libdispatch.dylib             	    0x7ff800166ac0 0x7ff800155000 + 72384
25  libdispatch.dylib             	    0x7ff800166526 0x7ff800155000 + 70950
26  CoreFoundation                	    0x7ff8003f5850 0x7ff800368000 + 579664
27  CoreFoundation                	    0x7ff8003f018b 0x7ff800368000 + 557451
28  CoreFoundation                	    0x7ff8003ef409 0x7ff800368000 + 553993
29  GraphicsServices              	    0x7ff80fcdd187 0x7ff80fcda000 + 12679
30  UIKitCore                     	    0x7ff805b703a2 0x7ff804b95000 + 16626594
31  UIKitCore                     	    0x7ff805b74e10 0x7ff804b95000 + 16645648
32  SwiftUI                       	    0x7ff84a61077e 0x7ff8495ce000 + 17049470
33  SwiftUI                       	    0x7ff84a610637 0x7ff8495ce000 + 17049143
34  SwiftUI                       	    0x7ff84a2bcaa5 0x7ff8495ce000 + 13560485
35  Diary                         	       0x10a4ffade static DiaryApp.$main() + 30
36  Diary                         	       0x10a500099 main + 9 (DiaryApp.swift:12)
37  dyld_sim                      	       0x10ab023ee start_sim + 10
38  dyld                          	       0x10cd9c3a6 0x10cd96000 + 25510

Without seeing more of the code it is hard to know for certain, but I would speculate that you need to setup your ModelContainer, then create your model object, insert it into the container's context, and save the context. Then set that container for your view. Something like this:

#Preview {
    let container = try! ModelContainer(for: DiaryModel.self, configurations: ModelConfiguration(isStoredInMemoryOnly: true))
    let context = container.mainContext

    let diaryModel = DiaryModel()
    context.insert(diaryModel)
    try! context.save()

    return DiaryReadView(diaryModel: diaryModel)
        .environmentObject(NavigationStateManager())
        .modelContainer(container)
}
Xcode 15 - Preview Crashes with error log but works when parent view is Pinned
 
 
Q