Tariq
Tariq

Reputation: 9979

Core Data To- Many Relationship

I am trying to prepare best possible design for following scenario.

Category -> List of Product -> Product Details

So, In product details screen user has an option to add the product to his cart. So, whenever user will open his cart then he can see all his product under Cart Controller. And user can again see product details screen from cart.

My question is how best I can prepare Cart Database ? Maintaining separate Cart Entity is the best solution ? Or I can have one bool value in product entity itself. But maintaining BOOL i think is not a best solution. For ex if i have 10,000 products then for only fetching Cart items i have to traverse complete product.

What best I think, maintaining seperate Cart entity and saving product_id in it. So, can you please tell me what kind a relationship it should be with Cart & Product ? Because Cart can't be independent I guess ?

Upvotes: 1

Views: 215

Answers (1)

Jesse Rusak
Jesse Rusak

Reputation: 57168

You probably want a many-to-many relationship from Cart to Product; perhaps called "contents" or something?

If the user only has a single cart at a time, you could also use the boolean in Product; if you mark that boolean as "indexed", it's not very expensive to fetch all products with it set to YES. (If you're using a SQLite backend.)

Upvotes: 2

Related Questions