Core Data 将超类实例转换为子类实例?

发布于 2024-12-22 00:28:38 字数 891 浏览 2 评论 0原文

以编程方式将 NSManagedObject 子类 (User) 实例转换为其子类 (AccountUser) 实例的最佳方法是什么?

设置

AccountUser 继承自 UserNSManagedObject

当我第一次注册或登录应用程序时,我成为 AccountUser< /代码>。然后,我下载所有朋友并将它们存储为 User 对象。

UserAccountUser 有属性 firstNamelastName 等。 AccountUser 有一些额外的东西,比如 accessToken.

问题

我的朋友约翰登录我的设备。由于他是我的朋友,因此他已被存储为用户。但现在,我想将他转换为 AccountUser。以编程方式执行此操作的最佳方法是什么?我有很多属性和关系需要保留,因此从 User 对象创建一个新的 AccountUser 对象,然后删除原始 User 对象是一个有很多事情要做。如果我只是创建一个 AccountUser 而不删除 User,事情就会变得混乱。例如,当我通过 ID 获取 User 时,我会返回两个对象:一个是 AccountUser,另一个是 User

What's the best way to programmatically convert an NSManagedObject-subclass (User) instance into an instance of its subclass (AccountUser)?

Setup

AccountUser inherits from User : NSManagedObject

When I sign up or log into the app for the first time, I become an AccountUser. Then, I download all of my friends and store them as User objects.

Both User & AccountUser have attributes firstName, lastName, etc. AccountUser has some extra things, like accessToken.

Problem

My friend John logs in on my device. Since he's my friend, he's already stored as a User. But now, I want to convert him into an AccountUser. What's the best way to do this programmatically? I have lots of attributes and relationships to preserve, so creating a new AccountUser object from a User object and then deleting the original User object is a lot to do. If I just create an AccountUser without deleting the User, things get messy. E.g. when I fetch User by ID, I get two objects back: one is the AccountUser, the other is the User.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪推晚风 2024-12-29 00:28:38

一种方法是简单地创建一个新的 AccountUser 对象并将相关字段从现有 User 对象复制到其中。然后适当地处理旧的 User 对象——删除它,保存它,任何有意义的事情。为 AccountUser 提供一个采用 User 实例的初始值设定项是有意义的,例如 -initWithUser:

另一个(可能更好)的选择是为所有用户使用单个类(用户)。如果需要为某些用户(当前为 AccountUsers)存储其他信息,请创建一个新的 Account 类并将 Account 实例与需要它的用户相关联。因此,代表您和您的朋友 John 的 User 对象都将具有关联的 Account 对象,而所有其他用户则不会。当您的其他朋友之一登录您的手机时,您可以为该人创建一个新的帐户对象,并将其与他们现有的用户对象关联。

One way to do it is to simply create a new AccountUser object and copy the relevant fields from the existing User object into it. Then deal with the old User object appropriately -- delete it, save it, whatever makes sense. It'd make sense to give AccountUser an initializer that takes an instance of User, like -initWithUser:.

Another (probably better) option would involve using a single class (User) for all users. If there's additional information to be stored for some users (currently AccountUsers), create a new Account class and associate an instance of Account with the users that require it. So the User objects that represent you and your friend John would each have associated Account objects, and all the other users wouldn't. When one of your other friends logs into your phone, you can create a new Account object for that person and associate it with their existing User object.

野却迷人 2024-12-29 00:28:38

也许您可以通过这种方式

  1. 为用户实体添加一个名为“is_account_user”的布尔属性。
  2. 设置用户和帐户用户之间的关系。
  3. 如果用户有帐户用户,则创建 AcountUser 实体并将其与用户关联。

如果用户没有帐户信息,则“is_account_user”将为NO;

当您获取用户时,您可以检查 is_account_user 是否为 YES,如果是,您可以通过关系获取他的 AcountUser。

希望有帮助

Maybe you can do it this way

  1. Add the User entity a boolean property called "is_account_user".
  2. Set a relationship between User and account user.
  3. if a user has an account user, create the AcountUser entity and relate it to the User.

If the user has no account info then the "is_account_user" will be NO;

When you fetch for a user you can check if is_account_user is YES, and if so you can get his AcountUser with the relationship.

Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文