Last active
January 22, 2024 01:15
-
-
Save benigumocom/9e500c9fc39ed09a82856fc880d5d3e2 to your computer and use it in GitHub Desktop.
【 #SwiftData 】ModelContainer の View へのセット 👉 https://android.benigumo.com/20240122/modelcontainer/
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 SwiftUI | |
import SwiftData | |
public extension View { | |
func backyardBirdsDataContainer(inMemory: Bool = true) -> some View { | |
modifier(DataContainerViewModifier(inMemory: inMemory)) | |
} | |
} | |
struct DataContainerViewModifier: ViewModifier { | |
private let container: ModelContainer | |
init(inMemory: Bool) { | |
container = try! ModelContainer( | |
for: DataGeneration.schema, | |
configurations: [ModelConfiguration(isStoredInMemoryOnly: inMemory)] | |
) | |
} | |
func body(content: Content) -> some View { | |
content | |
.onAppear { | |
DataGeneration.generateAllData(modelContext: container.mainContext) | |
} | |
.modelContainer(container) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment