Skip to content

Instantly share code, notes, and snippets.

@gallaugher
Created February 14, 2024 10:04
Show Gist options
  • Save gallaugher/9e0fa06ffbebcef7373a63085dcd70a8 to your computer and use it in GitHub Desktop.
Save gallaugher/9e0fa06ffbebcef7373a63085dcd70a8 to your computer and use it in GitHub Desktop.
PreviewContainer.swift
import Foundation
import SwiftData
class PreviewContainer {
let container: ModelContainer! // register the container
init(_ types: [any PersistentModel.Type], isStoredInMemoryOnly: Bool = true) {
let schema = Schema(types) // should be able to pass in any schema & have them registered
let config = ModelConfiguration(isStoredInMemoryOnly: isStoredInMemoryOnly)
self.container = try! ModelContainer(for: schema, configurations: [config])
}
func add(items: [any PersistentModel]) {
Task { @MainActor in
items.forEach {container.mainContext.insert($0)}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment