In my project I have some objects that I show from a server, lets call them Foo’s. When I get my Foo feed, I parse them into a NSMutableDictionary subclass called RemoteFoo, and pass these RemoteFoo objects all around the app to display data.
If the user ends up wanting to download a RemoteFoo, I then create a core-data NSManagedObject entity called Foo, and instantiate it using the values from the RemoteFoo. All this works.
The problem I have is that If I want a method to run on a RemoteFoo and a Foo, I have to duplicate it in both RemoteFoo.m and Foo.m. Also, my app if full of duplicate init’s like:
- (id)initWithFoo:(Foo *)foo;
- (id)initWithRemoteFoo:(RemoteFoo *)foo;
How can I avoid all of this code-duplication while still separating the temporary remote RemoteFoo from the core-data Foo entities that represent the Foo the user has saved locally?
You could just save all RemoteFoo’s as Foo’s and work with those. If you need different behavior, you could have an attribute on Foo indicating if it’s remote or not.