Wael
Wael

Reputation: 411

Many To Many with additional data using Swift and Core Data

I do a lot of apps (two tier apps) with SQL server, and this is my first app in iOS with core data.

I am trying to figure out how to do Many to Many relation with additional data. Let me explain that with simple example:

first Entity called List (referring list of products) and it has one attribute listName.

second entity called product (referring products) and it has one attribute productname.

Each "product" can be in more than one "List" and each "List" contain more than one "product".

Data Model

Xcode has created automatically the joined table:

SQLLITE

My problem now is I cannot figure out how to add for example a "quantity" column for this joined table. My main objective is to say:

In list1 I have 4 product1

In list2 I have 3 product1

In list2 I have 4 product2

...

In SQL server is too simple since I create my self this joined table and add additional data, but I can't figure out how to do the same thing in core data.

Upvotes: 3

Views: 1194

Answers (1)

user2782993
user2782993

Reputation: 193

Core Data hides the correlation table if you use Many-2-Many (without any additional attributes). This means, in the inverse way, that you need to create a correlation table yourself. You can do this, if you create a new Entity in Core Data, which should have a 2-One relationship to both other Entities.

Without extra attributes:

Entity1 <-> Entity2 (The correlation table is hidden, but is there)

With extra attributes:

Entity1 <- CorrelationEntity(with extra attributes) -> Entity2

Upvotes: 4

Related Questions