0

This is a two part question, here is the situation:

I have an app that lists a set of product models. The user should be able to select from this list the product models that they specifically own. They can own more than 1 of the same type of product and should also be able to set a custom name for the products they own. So I set up my data model with two entities. Product, and OwnedProduct. OwnedProduct inherits from the Product entity and adds a customName property.

Question 1: Is this the best way to implement the model or should I be doing something with relationships? Right now I don't have any relationships in place.

The next question involves actually creating an OwnedProduct record from one of the Products that has been selected by the user.

Question 2: Is there a way to take the selected Product object, duplicate it , and then cast it as an OwnedProduct? Or would I have to create a brand new instance of OwnedProduct and then manually assign all of it's properties from the properties of the Product instance?

Christian Schlensker
  • 21,708
  • 19
  • 73
  • 121

1 Answers1

2

Question 1: Is this the best way to implement the model or should I be doing something with relationships? Right now I don't have any relationships in place.

Do you plan to have multiple owners within the same context?

Can a single product be owned by different owners with different custom names?

If yes I would take a look at relationships. I would not duplicate a product because of an ownership. If the product changes, it probably should change for all owners.

A possible datamodel for your requirements: product ownership datamodel, three entities: Product, ProductOwnership and Owner

Question 2: Is there a way to take the selected Product object, duplicate it , and then cast it as an OwnedProduct? Or would I have to create a brand new instance of OwnedProduct and then manually assign all of it's properties from the properties of the Product instance?

As the clone will be a separate instance within your context you have to create a new one and copy all necessary properties. You can speed things up by iterating over NSEntityDescription properties and relationships. There is already a solution to cloning NSManagedObjects on SO

Community
  • 1
  • 1
Martin Brugger
  • 3,168
  • 26
  • 20
  • Actually this is a single user application, so there won't be multiple owners. Would this change your answer? Also I don't think the "amount" property would work because each individual owned product should have a custom name. Thanks for including a diagram. – Christian Schlensker Dec 27 '10 at 19:10
  • I agree with Martin that OwnedProduct should have a relationship with Product **instead** of OwnedProduct being a subclass/extension of Product. Design calls for a single Product being associated with multiple OwnedProducts. So, if there is no need for Owner, then to-many relationship is between Product and OwnedProduct. – westsider Dec 27 '10 at 19:53
  • Given the additional information I would change my recommendation. Your application contains a predefined product, but as soon as the user takes ownership over a product, these two products should no longer be connected, but instead independently editable? Within my datamodel, for multiple ownerships of a product you easily could create distinct ProductOwnership Objects to assign different custom names – Martin Brugger Dec 27 '10 at 22:22