NSObject正在被创造两次

时间:2011-06-03 19:19:37

标签: iphone objective-c core-data nsobject

我使用以下代码创建一个Session对象,并创建并添加对象Exercise和Set。

但现在我遇到的问题是,当我将练习添加到自定义日期(从UIDpatePicker中选择而不是今天的日期)时,它也会自动添加到今天的会话中。有什么想法吗?

我认为这种情况正在发生,因为加载视图时,默认为今天的日期然后创建集合,然后当您添加集合时,它将被添加到用户与今天的日期一起拍摄的自定义日期。

编辑:我认为是因为练习以与创建会话相同的方法添加到Session。因此,虽然练习可能没有一套,但会议仍然有这个练习

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

    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];
    }
    else
    {
        self.exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
        self.exercise.name = selectedExerciseName;
        [session addExercisesObject:exercise];
    }

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

-(IBAction)createSet
{    
    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];
}

更新代码:

- (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];
}

1 个答案:

答案 0 :(得分:0)

我不完全确定我理解你的代码,所以我可能错了......

我觉得奇怪的是:

if ([results count])
{
    session = (Session *)[results objectAtIndex:0];
}

根据时间戳进行提取后,得到第一个结果;否则你用self.pickerDate初始化会话。这可能是你将这个练习添加到今天的会议中的原因吗?

编辑:

在我看来,完全可以在创建会话的同一方法中将练习添加到会话中。您对分配给会话而不是分配的练习的说法可能是内部不一致的原因,但不是将练习添加到今天的会话中,除非Sessions和Sets之间存在某种关系从代码中可以看出(对我而言)。

您如何定义targetDateBegins, targetDateEnds?调用createSession时,这些值确定您是创建新会话还是重用现有会话。如果重用现有会话,则其日期已设置,并且将忽略选择器。

建议:在createSession的开头设置一个断点并调试你的程序,这样你就可以准确地看到createSession中发生了什么,并了解你的预期路径是否被采用或程序是否为你做了什么没想到。