如何在sqlite核心数据文件之间正确切换?(How to properly switch between sqlite core data files?)

编程入门 行业动态 更新时间:2024-10-27 07:22:46
如何在sqlite核心数据文件之间正确切换?(How to properly switch between sqlite core data files?)

所以我目前在coredata文件之间切换时遇到了一些麻烦。 方案如下:

1)有两个用户被授权使用我的应用程序,每个用户都有自己的sqlite文件来表示他们自己的数据,因为需要分离关注点2)当用户A注销并且用户B登录...核心数据时应切换到用户B的sqlite文件并仅在该文件中生效。

如果我删除用户A的PSC并为用户B创建一个新的PSC,我是在安全区还是我错过了更多的步骤? 我注意到用户A的一些数据最终出现在用户B的核心数据中。

So I am currently experiencing some troubles between switching between coredata files. The scenario is as follows:

1) There are two users authorized to use my app, and each user has their own sqlite file to represent their own data due to a need for separation of concerns 2) When User A logs out and User B logs in... core data should switch to User B's sqlite file and live within that file only.

If I remove User A's PSC and create a new one for User B, am I in the safe zone or am I missing more steps? I am noticing some data from User A ends up in User B's core data.

最满意答案

如果要在持久性存储文件之间切换,并且无法从一个连接到另一个的数据,则需要拆除整个Core Data堆栈,然后重新创建它。 这意味着:

所有NSPersistentStoreCoordinator 所有NSManagedObjectContext 您在应用中的任何位置从Core Data加载的所有托管对象

这些都需要用新实例替换,就像应用程序首次启动时一样。 因此,有时最好还拆除整个UI,从初始视图控制器重新加载所有内容。

如果您已将托管对象中的数据复制到其他位置(其他属性或UI元素),则还需要确保将其删除。

NSManagedObjectModel可以保留,因为它永远不会保存来自持久性存储的任何数据。

If you're going to switch between persistent store files and you can't have data from one connect to data from the other, you need to tear down the entire Core Data stack and then re-create it. That means:

All NSPersistentStoreCoordinators All NSManagedObjectContexts All managed objects that you have loaded from Core Data, anywhere in your app

Each of these needs to be replaced with a new instance, exactly as you would do when the app first launches. Because of this, it's sometimes best to also tear down the entire UI, reloading everything from the initial view controller.

If you've copied data from managed objects to other places (other properties or UI elements) you need to make sure to get rid of that as well.

The NSManagedObjectModel can stay, because it never holds any data from a persistent store.

更多推荐

本文发布于:2023-08-03 00:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   核心   文件   数据   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!