admin管理员组文章数量:1279008
Im running into an issue where swiftdata works perfectly and saves my data if I comment out any lines that call performSync
but as soon as I try to call performSync()
, SwiftData fails to save any changes whatsoever. I pass in modelContext
into the custom class from the @Enviroment
operator from a view onAppear
. How do I fix this?
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<Idea>()
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
}
}
}
.onAppear{
if let paId = UserDefaults.standard.string(forKey: DefaultsKeys.parentPageId){
if !completedSetupSync{
authManager.setupSyncManager(parentPageId: paId, modelContext: modelContext)
completedSetupSync = true
}
}
}
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.
本文标签: iosAccesing modelContext Outside of View Hiearchy Causing modelContext to not SaveStack Overflow
版权声明:本文标题:ios - Accesing modelContext Outside of View Hiearchy Causing modelContext to not Save - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741243360a2364410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论