所以我目前在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 appEach 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.
更多推荐
发布评论