为什么Core Data不插入此关系对象?

时间:2014-08-10 00:28:05

标签: ios objective-c core-data

我有一个对象currentWorkout,它是从上一个屏幕中的表中选择的,然后传递给当前视图。该对象是NSManagedObject的子类,类型为Workout

Workout.h

@class Movement;

@interface Workout : NSManagedObject
// ...
@property (nonatomic, retain) NSSet *movement;
@end

@interface Workout (CoreDataGeneratedAccessors)

- (void)addMovementObject:(Movement *)value;
- (void)removeMovementObject:(Movement *)value;
- (void)addMovement:(NSSet *)values;
- (void)removeMovement:(NSSet *)values;

@end

Workout.m只有@dynamic个属性的实现,没有添加/删除方法的代码。

在模态视图中创建新的Movement对象。 MovementList视图控制器在prepareForSegue:中创建托管对象,将其传递到填充信息的模式视图,然后使用MovementList将其返回到addMovementViewControllerDidSave:视图控制器。在这里保存managedObjectContext(如果用户取消添加)。

MovementListViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"addMovement"]){
        AddMovementViewController *amvc = (AddMovementViewController *)[segue destinationViewController];

        Movement *newMovement = (Movement *)[NSEntityDescription insertNewObjectForEntityForName:@"Movement" inManagedObjectContext:[self managedObjectContext]];
        amvc.movement = newMovement;
        amvc.delegate = self;

        [self.currentWorkout addMovementObject:newMovement];
    }
}

// ...

-(void)addMovementViewControllerDidSave:(Movement *)newMovement{
    [self dismissViewControllerAnimated:YES completion:nil];
    NSLog(@"New movement: %@",newMovement.name);

    NSError *error;
    if(![[self managedObjectContext] save:&error]){
        NSLog(@"Error: %@",error);
    }

    NSMutableSet *allMovements = [self.currentWorkout mutableSetValueForKey:@"movements"];
    NSArray *movementsArray = [allMovements allObjects];
    for(Movement *test in movementsArray){
        NSLog(@"%@",test.name);
    }

    [self.delegate newMovementAdded:newMovement];
}

问题是永远不会调用for循环,因为movementsArrayNULL。如果我尝试选择创建的第一个对象,那么也会返回NULL。这是一个问题,我正在测试是否添加了对象,还是更复杂的东西?

1 个答案:

答案 0 :(得分:1)

您在Workout上的多人财产似乎是“运动”,'但是你正在访问关键的动作。'这可能是问题吗?