I'm struggling to figure out how to build a Core Data model to support the class structure I've created in my app.
我正在努力弄清楚如何构建一个核心数据模型来支持我在我的应用程序中创建的类结构。
My app has users, and I allow those users to log into the app using multiple different authentication methods. Each authentication method requires slightly different credentials and cred types. I've chosen to solve this by creating an abstract AuthenticationSettings
base class, then subclass it with FacebookAuthenticationSettings
, TwitterAuthenticationSettings
, MyWebsiteAuthenticationSettings
, etc.
我的应用程序有用户,我允许这些用户使用多种不同的身份验证方法登录应用程序。每种身份验证方法都需要略有不同的凭据和信用类我选择通过创建一个抽象的AuthenticationSettings基类来解决这个问题,然后使用FacebookAuthenticationSettings,TwitterAuthenticationSettings,MyWebsiteAuthenticationSettings等对其进行子类化。
My User
class has a property authSettings
(of type AuthenticationSettings
), that stores an instance of one of the settings classes. Users are allowed only one auth method per user, so this works great.
我的User类有一个属性authSettings(类型为AuthenticationSettings),它存储一个设置类的实例。用户每个用户只允许使用一种身份验证方法,因此效果很好。
The question is, how do I create a Core Data model for my users given that authSettings
can have one of many object types stored to it?
问题是,如果authSettings可以存储多种对象类型中的一种,我如何为我的用户创建一个Core Data模型?
-
Do I create separate entities for each type of authentication and then create relationships between those entities and the user entity? This is strange because the data model doesn't enforce 1:1 auth method to user. When fetching users, I'd also have to fetch their corresponding auth methods.
我是否为每种类型的身份验证创建单独的实体,然后在这些实体和用户实体之间创建关系?这很奇怪,因为数据模型不会对用户强制执行1:1 auth方法。在获取用户时,我还必须获取相应的auth方法。
-
Do I just create my own serialization encoding of the auth creds and jam it into a string field on the user entity? This forces me to invent some encoding which feels especially messy if any future auth methods use fields that are difficult to serialize to string.
我只是创建自己的auth creds序列化编码并将其插入用户实体的字符串字段中吗?这迫使我发明一些编码,如果任何未来的auth方法使用难以序列化为字符串的字段,感觉特别麻烦。
-
Is there a better way?
有没有更好的办法?
1 个解决方案
#1
2
One way is to set up a Core Data entity hierarchy that parallels your class hierarchy. Create an abstract entity in Core Data called AuthenticationSettings
and create a series of sub-entities for each authentication type. As with your User
class property, create a relationship to AuthenticationSettings
.
一种方法是设置与您的类层次结构平行的Core Data实体层次结构。在Core Data中创建一个名为AuthenticationSettings的抽象实体,并为每种身份验证类型创建一系列子实体。与User类属性一样,创建与AuthenticationSettings的关系。
A simpler approach that sounds like it would meet your needs would be to make your various authentication classes conform to NSCoding
, and then just store them in a single "transformable" (not string) field on the user entity. Core Data would then convert the authentication object to/from NSData
automatically.
一个听起来更符合您需求的简单方法是使您的各种身份验证类符合NSCoding,然后将它们存储在用户实体的单个“可转换”(非字符串)字段中。然后,Core Data会自动将身份验证对象转换为NSData或从NSData转换。
#1
2
One way is to set up a Core Data entity hierarchy that parallels your class hierarchy. Create an abstract entity in Core Data called AuthenticationSettings
and create a series of sub-entities for each authentication type. As with your User
class property, create a relationship to AuthenticationSettings
.
一种方法是设置与您的类层次结构平行的Core Data实体层次结构。在Core Data中创建一个名为AuthenticationSettings的抽象实体,并为每种身份验证类型创建一系列子实体。与User类属性一样,创建与AuthenticationSettings的关系。
A simpler approach that sounds like it would meet your needs would be to make your various authentication classes conform to NSCoding
, and then just store them in a single "transformable" (not string) field on the user entity. Core Data would then convert the authentication object to/from NSData
automatically.
一个听起来更符合您需求的简单方法是使您的各种身份验证类符合NSCoding,然后将它们存储在用户实体的单个“可转换”(非字符串)字段中。然后,Core Data会自动将身份验证对象转换为NSData或从NSData转换。