I’m currently using Swift Data with the following pattern:
- ModelContainer is initialized once in the global scope as a global variable.
- The container is injected into my app using .modelContainer(container) and accessed in views via the env variable.
- For all other use cases, including tasks and various system callbacks (async or not), I create a ModelContext on the fly from the global container and discard it after the specific execution path is finished.
- ModelContext instances and their associated models are not shared between tasks or jobs, but they may be used in async tasks without being tied to a specific actor.
Is there anything inherently wrong with this approach?
I’m unfortunately seeing fringe errors, such as EXC_BAD_ACCESS, KERN_INVALID_ADDRESS, and EXC_BREAKPOINT, mostly related to Swift Data. I’m trying to sanity-check my basic approach to determine whether the issue lies in the overall design or elsewhere.