3

对于在保存之前设置的实体,我有一个强制genericCall关系。日志显示不是 nil,但在尝试保存托管对象上下文时出现验证错误,说明未设置关系。PlaylistCallPlaylistCallgenericCall

什么可能导致在保存期间已经设置为 nil 的关系?

我已验证或尝试的内容:

  • 我使用共享单例作为整个应用程序的托管对象上下文,因此我肯定在同一个托管对象上下文中工作
  • GenericCall和之间存在反比关系PlaylistCall
  • 一时兴起,我尝试删除该关系,然后重新创建它,但这并没有起到任何作用,所以它看起来像是我的代码中的一个错误,而不是 Xcode 中的一个随机错误
  • 我尝试使用setValue:forKey而不是动态属性访问器(点表示法)没有变化

以下是日志:

2013-03-25 11:48:35.400 Project[30599:c07] Supposed to add <PlaylistCall: 0xa1be450> (entity: PlaylistCall; id: 0xa17dea0 <x-coredata:///PlaylistCall/tBB209BC5-EF9A-4DAE-9A55-0001B97499392> ; data: {
    delaySetting = 0;
    genericCall = "0xd4ad520 <x-coredata://7EB1AFB2-7B49-431E-9700-275307B4D1C1/GenericCall/p367>";
    orderInTable = 4;
    playlist = nil;
    repeatSetting = 0;
    repeatType = 0;
    volumeSetting = 100;
}) with generic call <GenericCall: 0xd4c3fd0> (entity: GenericCall; id: 0xd4ad520 <x-coredata://7EB1AFB2-7B49-431E-9700-275307B4D1C1/GenericCall/p367> ; data: {
    animalCategory = "0xc0aaa80 <x-coredata://7EB1AFB2-7B49-431E-9700-275307B4D1C1/AnimalCategory/p19>";
    callDescription = "Call Name";
    numberOfTimesOnPlaylist = 0;
    numberOfTimesPlayed = 0;
    playlistCall = "0xa17dea0 <x-coredata:///PlaylistCall/tBB209BC5-EF9A-4DAE-9A55-0001B97499392>";
    soundFile = "sound_file_name";
    timeLength = "0.21";
})
2013-03-25 11:48:35.402 Project[30599:c07] Error saving.  Reason(s): 
    PlaylistCall: The attribute 'genericCall' cannot be empty.

这是我设置和保存值的方式:

GenericCall *genericCall = [self.fetchedResultsController objectAtIndexPath:indexPath];
PlaylistCall *playlistCall = (PlaylistCall *)[NSEntityDescription insertNewObjectForEntityForName:@"PlaylistCall" inManagedObjectContext:commonContext.managedObjectContext];
[self.playlist addPlaylistCall:playlistCall withGenericCall:genericCall orderInTable:[self.playlist.playlistCalls count]];
// Note: all other attributes are setup via model defaults
[commonContext save];

我设置关系的 NSManagedObject 子类Playlist

- (void)addPlaylistCall:(PlaylistCall *)playlistCall withGenericCall:(GenericCall *)genericCall orderInTable:(int)orderInTable
{
    NSMutableOrderedSet *tempSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.playlistCalls];
    playlistCall.genericCall = genericCall;
    playlistCall.orderInTable = [NSNumber numberWithInt:orderInTable];
    NSLog(@"Supposed to add %@ with generic call %@", playlistCall, playlistCall.genericCall);
    [tempSet addObject:playlistCall];
    self.playlistCalls = tempSet;
}

我的通用托管对象上下文单例 ( CommonContext) 看起来像这样(是的,我已经验证了self.managedObjectContext不是 nil 并且不会更改):

// Saves the managed object context
- (BOOL)save
{
    NSError *anError;
    if ([self.managedObjectContext hasChanges] && ![self.managedObjectContext save:&anError]){
        [self handleSaveError:anError];
        return NO;
    } else {
        return YES;
    }
}

关于我可能做错的任何想法?这看起来很简单,我在我的应用程序的其他任何地方都使用相同的方法来设置关系,所以我必须缺少一些东西。谢谢你的时间!

4

1 回答 1

4

很难确定,但这里有一个符合症状的场景:

  1. GenericCall在创建新的PlaylistCall. 但这GenericCall 已经具有nil价值 for playlistCall
  2. playlistCall.genericCall = genericCall使用新的PlaylistCall.
  3. 已经存在的PlaylistCall实例将其genericCall关系设置为nil-- 因为这是一对一的关系,这是本例中的默认行为。
  4. 保存失败——不是因为PlaylistCall你创建了新的,这很好,而是因为前一个PlaylistCall留下了悬空genericCall关系。

如果发生这种情况,您需要检查现有的playlistCall并在保存更改之前对其进行合理的处理 - 很可能将其重新分配genericCall为指向其他内容,或者可能只是删除实例。

于 2013-03-25T16:37:41.040 回答