且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多个持久存储

更新时间:2023-01-06 12:27:29

我将有一个核心数据堆栈(使用 NSPersistentContainer )用于用户管理。该堆栈将具有基本帐户详细信息和sql文件的名称。 (仅存储sql文件名而不存储完整的url路径,因为在极少数情况下,例如iTunes还原,路径可能会更改)。这将用于登录或选择帐户页面。

I would have one core data stack (using NSPersistentContainer) for user management. This stack would have the basic account details and the name of the sql file. (Store just the sql filename NOT the full url path, as the path can change in rare circumstance such as an iTunes restore). This would be used for the login, or select account page.

然后,我将使用存储在用户帐户对象中的sql文件名设置第二个核心数据堆栈。这将是应用程序使用的主堆栈。如果您需要注销,请拆除第二个堆栈并重新开始。删除和添加存储是一个坏主意,因为它不会处理行缓存或其他浮动的托管对象。

Then I would setup a second core data stack using the sql file name that was stored in the user account object. This would be the main stack used by the application. If you need to logout, then tear down the second stack and start over. Removing and adding store is a bad idea, as it won't deal with the row cache or other managedObjects that are floating around.

或者您可以只拥有一个核心数据堆栈并管理关系,以便每个对象都属于一个用户对象。然后,您将管理抓取操作以仅查看属于正确用户的对象。

Or you could simply have one core data stack and manage the relationship so that every object belongs to a user object. Then you would manage your fetches to only look at objects belonging to the correct user.