对于在保存之前设置的实体,我有一个强制genericCall
关系。日志显示不是 nil,但在尝试保存托管对象上下文时出现验证错误,说明未设置关系。PlaylistCall
PlaylistCall
genericCall
什么可能导致在保存期间已经设置为 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;
}
}
关于我可能做错的任何想法?这看起来很简单,我在我的应用程序的其他任何地方都使用相同的方法来设置关系,所以我必须缺少一些东西。谢谢你的时间!