保存される前に設定されているエンティティに対して必須のgenericCall
関係があります。ログには、がnilではないことが示されていますが、関係が設定されていないことを示す管理対象オブジェクトコンテキストを保存しようとすると、検証エラーが発生します。PlaylistCall
PlaylistCall
genericCall
保存中にすでに設定されている関係がゼロになる原因は何ですか?
私が検証または試みたこと:
- アプリ全体の管理対象オブジェクトコンテキストとして共有シングルトンを使用しているため、同じ管理対象オブジェクトコンテキスト内で確実に作業しています
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;
}
}
私が間違っているかもしれないことについて何か考えはありますか?それは簡単に思えますし、関係を設定するためにアプリの他の場所でこれと同じアプローチを使用しているので、私が見逃しているものがなければなりません。御時間ありがとうございます!