leerob
leerob

Reputation: 3142

Core Data with Swift - Parent/child relationship

I'm trying to create a workout logging application using Swift. I have two entities, a "Workout" and "Lift". The "Lift" entity is supposed to be the child of the "Workout".

Whenever I create a new lift, I want it to be associated with that workout. Right now, my application will show every lift created regardless of the workout.

Here's an idea of my current Core Data model.


Entity: Workout

Attributes: lifts, name, numOfLifts

Entity: Lift

Attributes: liftName, sets, numOfSets


Looking through examples on SO, it seems like I need something where the lifts attribute of the workout class points to the lift entity. The problem was I couldn't find any examples using Swift.

Any help is appreciated, I can post code if needed or screenshots.

Upvotes: 1

Views: 2396

Answers (1)

Richard
Richard

Reputation: 1728

Have you setup a relationship between the Workout and Lift entities in XCode. If each Workout will have only one Lift then use One-to-One, otherwise if each Workout has many Lifts then use One-to-Many.

Once the link is established you just tell the new Lift Entity (at the point when you create it) which Workout it should be connected to.

See this thread for more information: Saving CoreData to-many relationships in Swift

Upvotes: 0

Related Questions