我有一个实体Session
。在我看来,实体Exercise
可能会添加到Session
。
在保存之前,我想检查是否与Session
有任何关系,如果没有,我会在保存之前删除该对象。
谁能告诉我怎么做?
编辑:
- (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];
}
答案 0 :(得分:0)
编辑多么不明确的问题......!下次请先发布代码。
您的NSPredicate
可能有误,请尝试:
[NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)",
startDate, endDate];
ENDOFEDIT
只需获取对象以确保其存在与否。
如果你的关系是Session 1->n Exercise
,那么你应该有像myExercise.session
这样的反向关系来轻松检查(获取)。或者您可以使用普通NSFetchRequest
...
另请注意,CoreData中存在“级联”删除功能,但我怀疑它对您的(子实体)案例有帮助。
没有更多的代码/细节,很难确定你的问题是什么以及如何解决它。