Post

Replies

Boosts

Views

Activity

SwiftData initializing Optional Array to Empty Array
I've been seeing something that I find odd when using two SwiftData models where if I have one model (book, in this case) that has an optional array of another model (page, in this case), the optional array starts out as set to nil, but after about 20 seconds it updates to being an empty array. I see it in Previews and after building. Is this expected behavior? Should I just assume that if there is an optional array in my model it will eventually be initialized to an empty array? Code is below. import SwiftUI import SwiftData @Model final class Book { var title: String = "New Book" @Relationship var pages: [Page]? = nil init(title: String) { self.title = title } } @Model final class Page { var content: String = "Page Content" var book: Book? = nil init() { } } struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var books: [Book] var body: some View { NavigationSplitView { List { ForEach(books) { book in NavigationLink { Text("\(book.title)") Text(book.pages?.debugDescription ?? "pages is nil") } label: { Text("\(book.title)") Spacer() Text("\(book.pages?.count.description ?? "pages is nil" )") } } } HStack { Button("Clear Data") { clearData() } Button("Add Book") { addBook() } } .navigationSplitViewColumnWidth(min: 180, ideal: 200) } detail: { Text("Select an item") } } private func clearData() { for book in books { modelContext.delete(book) } try? modelContext.save() } private func addBook() { let newBook = Book(title: "A New Book") modelContext.insert(newBook) } } @main struct BookPageApp: App { var sharedModelContainer: ModelContainer = { let schema = Schema([Book.self, Page.self]) let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false) do { return try ModelContainer(for: schema, configurations: [modelConfiguration]) } catch { fatalError("Could not create ModelContainer: \(error)") } }() var body: some Scene { WindowGroup { ContentView() } .modelContainer(sharedModelContainer) } } #Preview { ContentView() .modelContainer(for: Book.self, inMemory: true) }
1
0
88
2w