SwiftData crashed when I insert (XCode 15.0)

Hi all,

I am trying to persist some data using SwiftData, I have two models:

  • Page
  • Line

The models are connected by a relationship like this:

import SwiftData

@Model final class Page {
    var id: UUID
    var created_at: Date
    var title: String
    var pinned: Bool
    
    @Relationship(deleteRule: .cascade) var lines: [Line] = []
    
    init(id: UUID = UUID(), created_at: Date = .now, title: String = "", pinned: Bool = false) {
        self.id = id
        self.created_at = created_at
        self.title = title
        self.pinned = pinned
        self.lines = lines
    }
}
import SwiftData

@Model final class Line: Identifiable {
    var id: UUID
    var input: String
    var output: String
    var keyword: Bool
    
    @Relationship(inverse: \Page.lines) var page: Page
    
    init(id: UUID = UUID(), input: String = "", output: String = "", keyword: Bool = false, page: Page) {
        self.id = id
        self.input = input
        self.output = output
        self.keyword = keyword
        self.page = page
    }
}

I attached the modelCOntainer to the Window

        WindowGroup {
            ContentView()
        }
        .modelContainer(for: Page.self)

And in my ContentView I am able to query and I get an empty array:

    @Query(sort: [SortDescriptor(\Page.created_at, order: .reverse)], animation: .spring) var pages: [Page]

However, when I try to insert a page or a line my app crashes:

func addSample() {
        let page = Page()
        context.insert(page)
    }
CrashReporter Key:   8570AF55-7ED0-39AC-F6D4-0322CC485E65
Hardware Model:      Mac14,9
Process:             Equals [47925]
Path:                /Users/USER/Library/Developer/Xcode/UserData/Previews/Simulator Devices/2652B05A-811D-4E76-8C11-833748962497/data/Containers/Bundle/Application/23A01A2A-1FB4-4A4F-A542-74637D044F4C/Equals.app/Equals
Identifier:          com.jonathangiardino.Equals
Version:             1.0 (1)
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd_sim [43814]
Coalition:           com.apple.CoreSimulator.SimDevice.2652B05A-811D-4E76-8C11-833748962497 [90202]
Responsible Process: SimulatorTrampoline [64001]

Date/Time:           2023-11-04 17:32:37.5567 +0100
Launch Time:         2023-11-04 17:30:42.6200 +0100
OS Version:          macOS 13.5 (22G74)
Release Type:        User
Report Version:      104

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x000000018c985be8
Termination Reason: SIGNAL 5 Trace/BPT trap: 5
Terminating Process: exc handler [47925]

Triggered by Thread:  0

Kernel Triage:
VM - (arg = 0x0) pmap_enter retried due to resource shortage
VM - (arg = 0x0) pmap_enter retried due to resource shortage
VM - (arg = 0x0) pmap_enter retried due to resource shortage


Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libswiftCore.dylib            	       0x18c985be8 _assertionFailure(_:_:file:line:flags:) + 248
1   SwiftData                     	       0x1a89d5724 specialized static ModelContainer.currentContainer(_:) + 692
2   SwiftData                     	       0x1a8a4f5d0 _DefaultBackingData.init(for:) + 248
3   SwiftData                     	       0x1a8a5419c _DefaultBackingData.__allocating_init(for:) + 52
4   SwiftData                     	       0x1a8a11438 static PersistentModel.createBackingData<A>() + 72
5   Equals                        	       0x1023a6e84 Page.init(id:created_at:title:pinned:) + 488 (@__swiftmacro_6Equals4Page5ModelfMm_.swift:2)
6   Equals                        	       0x1023a6c8c Page.__allocating_init(id:created_at:title:pinned:) + 92
7   ContentView.1.preview-thunk.dylib	       0x10d21181c ContentView.__preview__addSampleLine() + 256 (ContentView.swift:350)
8   ContentView.1.preview-thunk.dylib	       0x10d214b6c ContentView.__preview__addNewLine() + 96 (ContentView.swift:246)
9   ContentView.1.preview-thunk.dylib	       0x10d21a5b8 closure #3 in closure #1 in closure #1 in ContentView.__preview__body.getter + 36 (ContentView.swift:111)

Can anyone give me a hnt of what I am doing wrong???

In Page, try this

@Relationship(deleteRule: .cascade, inverse: \Line.page) var pages: [Page]

In Line, simply try the following

var page: Page

No need for @Relationship here as SwiftData infers this.

SwiftData crashed when I insert (XCode 15.0)
 
 
Q