多个持久存储 [英] Multiple persistent stores

查看:56
本文介绍了多个持久存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据分别保存在不同的商店(用户个人资料)中。实现此目标的最佳方法是什么?我将在运行时使用持久性对象存储。我应该简单地移除()当前版本和 addPersistentStore ()来创建新实例或使用早期创建的实例。

I need to keep my data separately in different stores(user profiles). What is the best way to achieve this? I'm going to play with Persistent Object Stores in runtime. Should I simply remove() the current one and addPersistentStore() to make a new or to use the early created instance.

推荐答案

我将有一个核心数据堆栈(使用 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.

这篇关于多个持久存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆