0

I have an entity Session. In my view, entity Exercise may be added to Session.

Before I save, I want to check if there are any relationships with Session, so if not, I will delete the object before saving it.

Can anyone tell me how to do this?

Edit:

- (void)createSession
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *dateComponentsForToday = [calendar components:unitFlags fromDate:self.picker.date];
    [dateComponentsForToday setHour:0];
    [dateComponentsForToday setMinute:0];
    [dateComponentsForToday setSecond:0];
    NSDate *targetDateBegins = [calendar dateFromComponents:dateComponentsForToday]; 
    NSDate *targetDateEnds = [targetDateBegins dateByAddingTimeInterval:(60 * 60 * 24 - 1)];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat: @"(timeStamp >= %@ && timeStamp <= %@)", targetDateBegins, targetDateEnds]];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Session" inManagedObjectContext:managedObjectContext]];
    NSLog(@"timeStamp >= %@ && timeStamp <= %@", targetDateBegins, targetDateEnds);

    NSError *error = nil;
    NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    NSLog(@"Fetch error: %@", error);

    if ([results count])
    {
        session = (Session *)[results objectAtIndex:0];
    }
    else
    {
        session = (Session *)[NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:managedObjectContext];
        session.timeStamp = self.picker.date;
    }

    NSSet *filteredExercisesFromSession = [session.exercises filteredSetUsingPredicate:[NSPredicate predicateWithFormat: @"name == %@",selectedExerciseName]];
    if ([filteredExercisesFromSession count] > 0)
    {
        self.exercise = [filteredExercisesFromSession anyObject];
    }

    [fetchRequest release];
    self.fetchedResultsController = nil;
    [setsTableView reloadData];
}

-(IBAction)createSet
{    
    NSSet *filteredExercisesFromSession = [session.exercises filteredSetUsingPredicate:[NSPredicate predicateWithFormat: @"name == %@",selectedExerciseName]];

    if ([filteredExercisesFromSession count] > 0)
    {
        self.exercise = [filteredExercisesFromSession anyObject];
    }
    else
    {
        NSLog(@"exercise does not already exist");
        self.exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
        self.exercise.name = selectedExerciseName;
        self.exercise.muscleGroup = muscleName;
        [session addExercisesObject:exercise];
    }

    Set *set = (Set *)[NSEntityDescription insertNewObjectForEntityForName:@"Set" inManagedObjectContext:managedObjectContext];
    set.weight = [NSNumber numberWithFloat:weightSelected2];
    set.reps = [NSNumber numberWithInt:repSelected];
    set.timeStamp = self.picker.date;
    [self.exercise addSetsObject:set];

    NSError *error = nil;
    if (![managedObjectContext save:&error]) 
    {
        // Handle the error.
    }
    NSLog(@"error: %@", error);
    self.fetchedResultsController = nil; 
    [setsTableView reloadData];
}
2
  • Clarification: Do you mean that a new Session object is always being created (in the first if-block) even if one already exist? You should explain what you expect to happen and then what actually happens. We don't know what this code is supposed to do.
    – TechZen
    Jun 3, 2011 at 18:38
  • Sorry, let me explain better. So first if block checks if a Session exists, if it does, fetch it, otherwise create it. And the end of all the code, I want to check if there are any exercise object for the a particular Session with timeStamp as today's date.
    – user594161
    Jun 4, 2011 at 8:19

1 Answer 1

0

EDIT What an unclear question...! Next time please post code first.

Your NSPredicate might be wrong, try this :

[NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)",
                                   startDate, endDate];

ENDOFEDIT


Just fetch the object to ensure it exist or not.

If your relation is Session 1->n Exercise then you should have an inverse relationship like myExercise.session to check (fetch) easily. Or you can just use plain NSFetchRequest...

Also note that there are 'cascade' delete facilities in CoreData, but I doubt It could help in your (child entity) case.

W/o any more code/details it is hard to figure what's your issue and how to solve it.

2
  • Dear Vincent, I have added my code in the question. The problem I am facing here is that I keep getting a blank Session object for today's date every time I load the view. This is what I am trying to get rid of.
    – user594161
    Jun 4, 2011 at 8:42
  • Unforetunately, I am having the same problem of a Session keep being created for today's date along with the correct session with the date that was manually selected.
    – user594161
    Jun 4, 2011 at 8:58

Your Answer

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