Zugriff auf modelcontext außerhalb der Ansicht Hierarchie, die modellcontext nicht speichernIOS

Programmierung für iOS
Anonymous
 Zugriff auf modelcontext außerhalb der Ansicht Hierarchie, die modellcontext nicht speichern

Post by Anonymous »

Ich bin in ein Problem, in dem SwiftData perfekt funktioniert und meine Daten speichert, wenn ich alle Zeilen, die diese Anrufe Performsync kommentieren Änderungen überhaupt. Ich übergebene modelContext in die benutzerdefinierte Klasse aus dem @enviroment Operator aus einer Ansicht onAppear . Wie repariere ich das? < /P>
func performSync() async {
guard let notionService = notionService, isInitialized else { return }

// Set isSyncing on the main actor.
await MainActor.run {
isSyncing = true
}

do {
// Fetch the ideas on the main actor.
let ideas: [Idea] = try await MainActor.run {
let descriptor = FetchDescriptor()
return try modelContext.fetch(descriptor)
}

try await notionService.syncIdeas(ideas)

// Save changes on the main actor.
await MainActor.run {
try? modelContext.save()
isSyncing = false
}
} catch {
print("Sync failed: \(error)")
await MainActor.run {
isSyncing = false
}
}
}
< /code>
.onAppear{
if let paId = UserDefaults.standard.string(forKey: DefaultsKeys.parentPageId){
if !completedSetupSync{
authManager.setupSyncManager(parentPageId: paId, modelContext: modelContext)
completedSetupSync = true
}
}
}
< /code>
I was expecting the code to not effect other container changes outside the class and also be able to save changes to the context itself.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post