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.
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.