I think my understanding of ModelActor is wrong... I thought ModelActors are used to perform database actions that we don't want to run on the main thread and lock up the UI.
Are there any specific conditions that I need to be thinking about when trying to save a SwiftData object to the database?
I am getting the warning "Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates." even though I am not doing any UI related activity in the ModelActor. All I am doing is a bunch of SwiftData model updates and try modelContext.save()
Changing this line to MainActor.run{try modelContainer.mainContext.save()}
works fine but the issue is that it runs on the main thread which I dont want to do.
I tried creating a simple example, but that works fine without the message!! The code is huge and much of it is not under my control unfortunately!! I am sure I am missing something and I am unable to figure it out. Any pointers?? Has anyone come across this scenario with a modelActor?
Thanks for your help!
I found the reason for the warning. The explanation is posted in this forum : https://forums.developer.apple.com/forums/thread/765997 The issue started when I was observing the notifications from the changes to the SwiftData object and trying to save an object in a different thread started giving me the warning