admin管理员组文章数量:1336320
So, since I updated to Xcode 16.1 and iOS 18, my SwiftData project who relies on autosave doesn't work as expected.
Indeed, it works but only after a delay, around 30 seconds.
So if I add something to the database, I must wait around this delay to quit and relaunch the app, to see the changes persist.
I researched but didn't find any fix, besides using context.save() but since the method can throw errors, I cannot find exactly what errors and so, how to react to them.
If you have a solution, or just an idea, please let me know :)
Thanks a lot guys.
So, since I updated to Xcode 16.1 and iOS 18, my SwiftData project who relies on autosave doesn't work as expected.
Indeed, it works but only after a delay, around 30 seconds.
So if I add something to the database, I must wait around this delay to quit and relaunch the app, to see the changes persist.
I researched but didn't find any fix, besides using context.save() but since the method can throw errors, I cannot find exactly what errors and so, how to react to them.
If you have a solution, or just an idea, please let me know :)
Thanks a lot guys.
Share Improve this question asked Nov 20, 2024 at 12:50 JintaeJintae 537 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 1The autosave happens every 60 seconds after the app launches, and when the app goes into the background.
You can still call modelContext.save()
to do an immediate save after an update.
Re-running the app with the debugger kills the currently running app immediately, so unsaved data is lost. You can work around the loss by sending the app to the background by hand
Personally I have my own autoSave
function I attach to the View
ForEach(projects) { project in
NavigationLink(project.name, value: project)
.autoSave(project) // <- here
}
And it looks something like this
extension View {
func autoSave<S>(_ model: S) -> some View where S : SavableProtocol & Observable {
modifier(AutoSaveVM(model: model))
}
}
struct AutoSaveVM<S>: ViewModifier where S : SavableProtocol & Observable {
let model: S
@State private var error: Error?
func body(content: Content) -> some View {
content
.task(id: model.hasChanges, {
do {
try model.save()
} catch {
self.error = error
}
})
.alert(error: $error)
}
}
The Observable
part will call .task(id: model.hasChanges
whenever there are changes.
本文标签: iosSwiftData 30 seconds delay on autosaveStack Overflow
版权声明:本文标题:ios - SwiftData 30 seconds delay on autosave - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742355563a2459336.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
print(error)
in the catch to start with to determine what error you get? Then you can add better error handling later on but that is another issue. That said if your call to save() throws an error then the automatic save will also generate an error or there is something wrong with how you save. But again this is another issue. – Joakim Danielson Commented Nov 21, 2024 at 11:05