Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active July 12, 2024 05:54
Show Gist options
  • Save benigumocom/d4663a6607a2dc46eda432bd769a204e to your computer and use it in GitHub Desktop.
Save benigumocom/d4663a6607a2dc46eda432bd769a204e to your computer and use it in GitHub Desktop.
【Swift】Apple 公式サンプル にみる @Modelactor を singleton にして ViewModifier にすると便利 👉 https://android.benigumo.com/20240711/modelactor-singleton/
import SwiftUI
import SwiftData
@ModelActor
actor LocalData {
nonisolated(unsafe) private(set) static var shared: LocalData! // *
private var task: Task<Void, Never>?
static func createInstance(modelContainer: ModelContainer) { // *
shared = LocalData(modelContainer: modelContainer)
}
func load() async {
}
func update() {
task = Task {
}
}
func cancel() {
task?.cancel()
}
func status() -> Status { // ?
}
nonisolated func fetch() sync -> [Item] { // *
(try? modelContext.fetch(FetchDescriptor<Item>())) ?? []
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment