Is that possible to update ModelContainer?

  • Here is what I thought

I want to give each user a unique container, when the user login or register, the user could isolate their data in specific container.

I shared the container in a singleton actor, I found it's possible to update the container in that actor.

But I think it won't affect the modelContext which is in the Environment.

Does SwiftData allow me or recommend to do that?

Answered by DTS Engineer in 864654022

Technically, it's totally fine that your app replaces an existing model container with a new one, or use multiple containers. Just be sure that you update with the appropriate container for your views and scene via their modelContainer(_:) method. When that is done, the model context injected to the environment of the view hierarchy should change automatically, which triggers an update for the view hierarchy.

Practically, I'd probably only do that if every user has their own store (so I need to switch to another store when a user login happens). Sharing one single model container across an app is good enough for most cases, and makes things simpler.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Accepted Answer

Technically, it's totally fine that your app replaces an existing model container with a new one, or use multiple containers. Just be sure that you update with the appropriate container for your views and scene via their modelContainer(_:) method. When that is done, the model context injected to the environment of the view hierarchy should change automatically, which triggers an update for the view hierarchy.

Practically, I'd probably only do that if every user has their own store (so I need to switch to another store when a user login happens). Sharing one single model container across an app is good enough for most cases, and makes things simpler.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thank you, I tried, and I think the key is modelContainer(_:) method

Now I have login and register view inside my MainScreenView. MainScreenView is the top View, which uses modelContainer(_:) method inject the container to the View, and the container is saved in a singleton actor.

So the injection is finished right now, although I changed the container in singleton actor, that's the reason it won't affect anything.

I think what I should do is, separate the inject, rearrange the hierarchy, make sure when MainScreen is open it could access the new Container, am I right?


BTW, I have some data which happened before register, when registration is done, I want to save that data to a new Container, and I got illegal inject error.

So it's not possible to save the model data from another ModelContext to a new ModelContext, is that right? I should save them to another format then transfer to Model Data.

So the injection is finished right now, although I changed the container in singleton actor, that's the reason it won't affect anything.

You assessment sounds about right. If your model container is a @State of the view, as shown in the API specification I linked above, and the view injects the container to a subview hierarchy, I'd expect that changing container triggers an update of the subview with the new container.

So it's not possible to save the model data from another ModelContext to a new ModelContext, is that right?

Right. If you save a model instance with a model context, and need to access the model instance from a second context, consider grabbing the persistentModelID of the instance and converting it to a new instance with the second context using model(for:).

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thank you, and I made it, the code is still simple and intuitive.

再次感谢 boss 陈,祝早日当上大锦鲤 :P

Is that possible to update ModelContainer?
 
 
Q