lukacs-m/SimplyPersist

Warning upon initialisation

Closed this issue · 2 comments

Hi,
When I try to initialise the library, I get a warning (with strict concurrency checks enabled):
Passing argument of non-sendable type 'ModelConfiguration' into actor-isolated context may introduce data races

context:

.onTapGesture {
            Task.detached {
                let modelConfiguration = ModelConfiguration(isStoredInMemoryOnly: false)
                let persistenceService = try! PersistenceService(with: ModelConfiguration(for: User.self, isStoredInMemoryOnly: false))
                // do background work here
              }

Also, I was wondering if this library is supposed to work in conjunction with @query? As in, when I manipulate objects using the library, will @querys elsewhere in my code pick up the changes?

Thanks!

Hello @bastiaanterhorst,

This library was designed to move away from the "AppleMagic" aspects of SwiftData. As such, tools like the @query macro won't work here. You'll need to handle things manually, which gives you more control but also means more boilerplate code.

If you prefer using View-based macros, I would recommend sticking with Apple's SwiftData. Personally, I dislike having logic in my views (call me old-fashioned) and relying solely on reference-based items to convey data changes. I prefer my models to be structs whenever possible, which @model and @query don't allow.

Regarding the warning, I see you're creating a persistence service inside a tap gesture and a detached Task. This approach seems counterintuitive to me. It could lead to multiple services running simultaneously if someone clicks the button multiple times and the tasks take time to finish. Ideally, the local database service should be created at the start of the app or only once, then dispatched where needed using a dependency injection tool.

For the specific warning, ModelConfiguration was not made sendable by Apple, as seen in its declaration: public struct ModelConfiguration: Identifiable, Hashable. This might be addressed in upcoming SwiftData changes.

I hope this clears up some of your questions.

Thanks for your response, it makes sense! I'll need to look more into using viewmodels and relying less on @query.