Multible saved accounts and SwiftData

For a CRM application, I want users to be able to switch between accounts and have their saved contacts stored locally. Whenever a user logs in, the app should fetch data from their specific database location.

What’s the best practice to achieve this?

Should I create a separate database for each user?
Should I store all the data in one database and filter it by user?
Or is there a better approach I should consider?

I'd probably choose to centralize the data in one store (and filter the data with the user info).

Technically, there is nothing preventing you from creating a store (with a different store URL) per user – You can do that by creating a new model container (ModelContainer) using the appropriate store URL when a user logs in, and refreshing the whole view hierarchy to make sure it uses the new container.

However, the number of users can grow over time, and so your might eventually have a lot of store files in the file system, which may become hard to manage. Centralizing the data in one store provides better scalability in that regard.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Multible saved accounts and SwiftData
 
 
Q