Monday 9 September 2013

One to Many Core data Model Additions

One to Many Core data Model Additions

I have a core data model with four entities. Each entity is related to the
other with a one to many relationship.
RecordDate
Attribute: date
Relationship: records (one to many with RecordWorkout)
RecordWorkout
Attribute: recWorkoutName
Relationship: recExercises (One to many with RecordExercise)
recordDate (One to one Inverse with RecordDate)
RecordExercise
Attribute: recExerciseName
Relationship: sets (One to many with RecordSet)
recordExercise (One to one Inverse with RecordExercise)
RecordSet
Attribute: set, weight, reps
Relationship: recordExercise (one to one Inverse with RecordExercise)
How my Code should function:
Upon first running the application, there is no data stored.
When the user presses the Log Button.. All four Entities data is available
to be saved.
So a fetch is done for RecordData, which returns nothing, therefore a
fresh save is done for the four entities. Shown Below:
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]
initWithEntityName:@"RecordDate"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date == %@",
dateLabel.text];
[fetchRequest setPredicate:predicate];
fetchRequest.fetchLimit = 1;
self.recorddates = [[managedObjectContext executeFetchRequest:fetchRequest
error:nil] mutableCopy];
self.recordDate = [[managedObjectContext executeFetchRequest:fetchRequest
error:nil] mutableCopy];
if ([self.recorddates count] == 0)
{
NSLog(@"No Dates Imported");
RecordDate *newRecordDate = (RecordDate *)[[NSManagedObject alloc]
initWithEntity:recordDateEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordDate.date = dateLabel.text;
RecordWorkout *newRecordWorkout = (RecordWorkout *)[[NSManagedObject
alloc]
initWithEntity:recordWorkoutEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordWorkout.recWorkoutName = testLabel2.text;
[newRecordDate addRecordsObject:newRecordWorkout];
RecordExercise *newRecordExercise = (RecordExercise
*)[[NSManagedObject alloc] initWithEntity:recordExerciseEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordExercise.recExerciseName = exNameLabel.text;
[newRecordWorkout addRecExercisesObject:newRecordExercise];
RecordSet *newRecordSet = (RecordSet *)[[NSManagedObject alloc]
initWithEntity:recordSetEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordSet.setLog = setStr;
newRecordSet.weight = weight;
newRecordSet.reps = reps;
[newRecordExercise addSetsObject:newRecordSet];
NSLog(@"Set number one should be added to model: %@", setStr);
}
else {....
Now when the log button is pressed again, the record date fetch will
return a date and the above if statement will not be entered. The data for
RecordDate, RecordWorkout, RecordExercise. Remains the same, but the data
for RecordSet changes. The next part of the code within the else statement
is shown below....this part functions correctly when the user runs the
application for the first time, and presses the log button as many times
as they wish.
} else {
NSLog(@"Dates Are Imported: %@", [self.recorddates objectAtIndex:0]);
NSLog(@"Number of Dates in Array: %lu", (unsigned
long)[self.recorddates count]);
RecordDate *prevRecordDate = (RecordDate *)[self.recorddates
objectAtIndex:0];
NSFetchRequest *fetchRWRequest = [[NSFetchRequest alloc]
initWithEntityName:@"RecordWorkout"];
NSPredicate *predicateRW = [NSPredicate
predicateWithFormat:@"recWorkoutName == %@", testLabel2.text];
[fetchRWRequest setPredicate:predicateRW];
fetchRWRequest.fetchLimit = 1;
self.recordworkouts = [[managedObjectContext
executeFetchRequest:fetchRWRequest error:nil] mutableCopy];
if ([self.recordworkouts count] == 0)
{
NSEntityDescription *recordWorkoutEntity = [NSEntityDescription
entityForName:@"RecordWorkout"
inManagedObjectContext:self.managedObjectContext];
NSEntityDescription *recordExerciseEntity = [NSEntityDescription
entityForName:@"RecordExercise"
inManagedObjectContext:self.managedObjectContext];
NSEntityDescription *recordSetEntity = [NSEntityDescription
entityForName:@"RecordSet"
inManagedObjectContext:self.managedObjectContext];
RecordWorkout *newRecordWorkout = (RecordWorkout
*)[[NSManagedObject alloc]
initWithEntity:recordWorkoutEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordWorkout.recWorkoutName = testLabel2.text;
[prevRecordDate addRecordsObject:newRecordWorkout];
RecordExercise *newRecordExercise = (RecordExercise
*)[[NSManagedObject alloc] initWithEntity:recordExerciseEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordExercise.recExerciseName = exNameLabel.text;
[newRecordWorkout addRecExercisesObject:newRecordExercise];
RecordSet *newRecordSet = (RecordSet *)[[NSManagedObject alloc]
initWithEntity:recordSetEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordSet.setLog = setStr;
newRecordSet.weight = weight;
newRecordSet.reps = reps;
[newRecordExercise addSetsObject:newRecordSet];
}
else {
NSLog(@"Workouts Are Imported: %@", [self.recordworkouts
objectAtIndex:0]);
NSLog(@"Number of Workouts in Array: %lu", (unsigned
long)[self.recordworkouts count]);
RecordWorkout *prevRecordWorkout = (RecordWorkout
*)[self.recordworkouts objectAtIndex:0];
NSFetchRequest *fetchRERequest = [[NSFetchRequest alloc]
initWithEntityName:@"RecordExercise"];
NSPredicate *predicateRE = [NSPredicate
predicateWithFormat:@"recExerciseName == %@", exNameLabel.text];
[fetchRERequest setPredicate:predicateRE];
fetchRERequest.fetchLimit = 1;
self.recordexercises = [[managedObjectContext
executeFetchRequest:fetchRERequest error:nil] mutableCopy];
if ([self.recordexercises count] == 0) {
NSEntityDescription *recordExerciseEntity =
[NSEntityDescription entityForName:@"RecordExercise"
inManagedObjectContext:self.managedObjectContext];
NSEntityDescription *recordSetEntity = [NSEntityDescription
entityForName:@"RecordSet"
inManagedObjectContext:self.managedObjectContext];
RecordExercise *newRecordExercise = (RecordExercise
*)[[NSManagedObject alloc] initWithEntity:recordExerciseEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordExercise.recExerciseName = exNameLabel.text;
[prevRecordWorkout addRecExercisesObject:newRecordExercise];
RecordSet *newRecordSet = (RecordSet *)[[NSManagedObject alloc]
initWithEntity:recordSetEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordSet.setLog = setStr;
newRecordSet.weight = weight;
newRecordSet.reps = reps;
[newRecordExercise addSetsObject:newRecordSet];
} else {
NSLog(@"Exercises Are Imported: %@", [self.recordexercises
objectAtIndex:0]);
NSLog(@"Number of Exercises in Array: %lu", (unsigned
long)[self.recordexercises count]);
RecordExercise *prevRecordExercise = (RecordExercise
*)[self.recordexercises objectAtIndex:0];
NSEntityDescription *recordSetEntity = [NSEntityDescription
entityForName:@"RecordSet"
inManagedObjectContext:self.managedObjectContext];
RecordSet *newRecordSet = (RecordSet *)[[NSManagedObject alloc]
initWithEntity:recordSetEntity
insertIntoManagedObjectContext:self.managedObjectContext];
newRecordSet.setLog = setStr;
newRecordSet.weight = weight;
newRecordSet.reps = reps;
[prevRecordExercise addSetsObject:newRecordSet];
NSLog(@"next Set added: %@", setStr);
}
}
self.recorddates = [NSMutableArray arrayWithObjects:nil];
self.recordworkouts = [NSMutableArray arrayWithObjects:nil];
self.recordexercises = [NSMutableArray arrayWithObjects:nil];
The problem occurs when close the view in question...and return back to
the view but only change the date to the next day (RecordWorkout data and
RecordExercise data remains the same). When log is pressed the first part
of the if statement is entered and a new date is saved in the model with
the corresponding data for the other entities...
But when i press log again...the other entity data is then added to the
first date added previously. and not the second date.
From what i can see the error is occurring when i am fetching the
RecordExercises...as it is fetching the recordExercise that was initially
added...not the recordExercise added for the new date.
If anyone can help me solve this...i would be really grateful. Thank You.
Edit:
Forgot to mention: date, recWorkoutName and recExerciseName (Attributes)
within one object cannot have duplicate entries. if that makes sense :S

No comments:

Post a Comment