Last active
July 12, 2024 05:54
-
-
Save benigumocom/d4663a6607a2dc46eda432bd769a204e to your computer and use it in GitHub Desktop.
【Swift】Apple 公式サンプル にみる @Modelactor を singleton にして ViewModifier にすると便利 👉 https://android.benigumo.com/20240711/modelactor-singleton/
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 | |
@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