Created
February 14, 2024 10:04
-
-
Save gallaugher/9e0fa06ffbebcef7373a63085dcd70a8 to your computer and use it in GitHub Desktop.
PreviewContainer.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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