1

I have two Core Data entities called PreviousWorkout, and ExerciseData. The PreviousWorkout entity holds the following attributes: date, workoutName, and exercises. exercises is supposed to be an array of ExerciseData. After making the class with Editor > Create NSManagedObject Subclass, I then did the same thing with the ExerciseData entity.

I'm unsure whether or not this is the correct way of storing data using Core Data. My goal is to have a PreviousWorkout which contains an array of ExerciseData as well as date and workoutName.

The way I'm creating it in code is by creating a managedbjectContext with:

let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext

Then I create a PreviousWorkout array with:

let newItem = NSEntityDescription.insertNewObjectForEntityForName("PreviousWorkout", inManagedObjectContext: managedObjectContext!) as PreviousWorkout

I add all my stuff (date and workoutName) to newItem, then I attempt to create an array of ExerciseData with:

var exerciseData = NSEntityDescription.insertNewObjectForEntityForName("ExerciseData", inManagedObjectContext: managedObjectContext!) as [ExerciseData]

The program crashes at this point. I realize that this last line is very wrong but I'm not sure how to create an array of ExerciseData. After creating the array I would like to append to it, and then set values for its attributes before setting the newItem.exercises equal to the ExerciseData object.

Is this the correct way of creating a Core Data object that contains an array? How do I create the array of [ExerciseData]?

Thanks in advance

1 Answer 1

2

An entity is essentially a table so you would store many Workouts in it and create a Workout entity, not just a PreviousWorkout entity. Then you should have an Exercise entity and store many exercises in it. Then to store an "array" in Core Data, you make a relationship between the two entities (probably a many to many relationship because each workout can have many exercises and each exercise can be a part of many workouts). Then to add exercises to a workout object, you just append them to a Workout objects exercises property which is an NSSet and it points at Exercise objects.

So now you have many workout objects and exercise objects, you can fetch the previous workout by using a fetch request and sorting all of your workouts by date or whichever property you want to sort by and taking the first result of the fetch request.

You create a new exercise like so:

let exerciseData = NSEntityDescription.insertNewObjectForEntityForName("Exercise", inManagedObjectContext: managedObjectContext!) as Exercise
Sign up to request clarification or add additional context in comments.

4 Comments

thanks! Is my method of creating a new workout (newItem) correct?
Yes, that is correct, but I would advise renaming "PreviousWorkout" to "Workout"
alright I will do that. One last thing, you mentioned that the exercises property is an NSSet. How do I go about setting that up? It is currently set as Transformable in the data model
It isn't supposed to be of any type. You have to set up a relationship in your data model. Then, when you regenerate you NSManagedObject subclasses, you will see the relationship property of type NSSet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.