3

保存される前に設定されているエンティティに対して必須のgenericCall関係があります。ログには、がnilではないことが示されていますが、関係が設定されていないことを示す管理対象オブジェクトコンテキストを保存しようとすると、検証エラーが発生します。PlaylistCallPlaylistCallgenericCall

保存中にすでに設定されている関係がゼロになる原因は何ですか?

私が検証または試みたこと:

  • アプリ全体の管理対象オブジェクトコンテキストとして共有シングルトンを使用しているため、同じ管理対象オブジェクトコンテキスト内で確実に作業しています
  • 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。の値がありませんplaylistCall
  2. playlistCall.genericCall = genericCall新しいを使用して割り当てを行いPlaylistCallます。
  3. これは1対1の関係であり、この場合のデフォルトの動作であるため、既存のPlaylistCallインスタンスのgenericCall関係は--に設定されています。nil
  4. 保存は失敗しPlaylistCallます-作成した新しいものが原因ではなく、以前の関係PlaylistCallがぶら下がっていることが原因ですgenericCall

これが発生している場合は、playlistCall変更を保存する前に、既存のものを確認し、適切な処理を行う必要がgenericCallあります。おそらく、他の何かを指すように再割り当てするか、インスタンスを削除するだけです。

于 2013-03-25T16:37:41.040 に答える