2011-11-12 10 views
0

2つのエンティティでモデルを作成しました。エンティティAは、エンティティBとの関係(1対1)を持っています。私の問題は、関係のデフォルト値を設定する方法がわかりません。エンティティに新しいエントリを追加すると、関係の値に「値なし」が返されます(対応するNSArrayControlerまたは[newObjectEntityA initWithEntity:....]に送信されるアクション)。属性のような関係をデフォルト値コアデータ内のリレーションシップのデフォルト値

例:。もちろん

Entity A 
attrib a (has a default value) 
attrib b (has a default value) 
relation to B (has no default value) 

は、この問題を回避するには、次のようなものです:。

[newObjectEntityA setValue: defaultValueInEntityB forKey:@"relationToB"]; 

が、私は、より便利なソリューションが存在しなければならないと思います

ありがとうございます。 トーマス

答えて

0

大丈夫...問題が解決しました(多かれ少なかれ)。私がやったことはNSManagedObjectのサブクラスを作成することであり、私はこの方法オーバーライド - (無効)initWithEntity:LocoType」実体の最初のレコードに

@implementation Locos 
static NSManagedObject *defaultType; 

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context 
{ 
    self=[super initWithEntity:entity insertIntoManagedObjectContext:context]; 

    if(self==nil) 
    { 
     [self release]; 
     return(nil); 
    } 

    if(defaultType==nil) 
    { 
     NSEntityDescription* locoType=[NSEntityDescription entityForName:@"LocoType" inManagedObjectContext:context]; 
     if(locoType==nil) return(self); 

     NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
     [request setEntity:locoType]; 
     NSPredicate *predicate=[NSPredicate predicateWithFormat:@"Default==TRUE"]; 
     [request setPredicate:predicate]; 
     [context executeFetchRequest:request error:nil]; 

     NSArray *array=[context executeFetchRequest:request error:nil]; 

     defaultType=[array objectAtIndex:0]; 
    } 

    if(defaultType!=nil) 
    { 
     [self setPrimitiveValue: defaultType forKey:@"Type"]; 
    } 

    return(self); 
} 
@end 

それは実体に関係「タイプ」を設定「ロコスを」:insertIntoManagedObjectContext "Default"属性がtrueに設定されています。それは動作しますが、私が理解していないことがあります。この場合には

@implementation Locos 

- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context 
{ 
    self=[super initWithEntity:entity insertIntoManagedObjectContext:context]; 

    if(self==nil) 
    { 
     [self release]; 
     return(nil); 
    } 

    NSEntityDescription* locoType=[NSEntityDescription entityForName:@"LocoType" inManagedObjectContext:context]; 
    if(locoType==nil) return(self); 

    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; 
    [request setEntity:locoType]; 
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"Default==TRUE"]; 
    [request setPredicate:predicate]; 
    [context executeFetchRequest:request error:nil]; 

    NSArray *array=[context executeFetchRequest:request error:nil]; 

    [self setPrimitiveValue: [array objectAtIndex:0] forKey:@"Type"]; 

    return(self); 
} 
@end 

私は追加するバインドされているボタンを押した場合、::私はに方法を簡素化する場合、私は非常に奇妙な効果を得る(エンティティ「ロコス」にバインドされている)アレイコントローラの私はいつも2つの新しいエントリを得る!私はデバッガを実行し、メソッドinitWithEntryは一度だけ呼び出されます。私は、レコードI静的変数を格納する最初のコード例のように一度だけ呼び出されたフェッチの場合にも発生しませんライン

[context executeFetchRequest:request error:nil]; 

を削除するように私はまた、すぐに問題を取り除きます。

誰でも何が起こるか考えていますか?

+0

Awakefrominsertは上書きするためのより良い方法だと私は思います。 – jrturton

+0

こんにちはjrturton!それは良い方法です!ありがとう! –

+0

さて、私は非常に短い答えとしてそれを置くよ! – jrturton

1

私はFactoryを作成し、Aの新しいオブジェクトを作成し、必要に応じて事前設定します。

3

代わりにawakeFromInsertを上書きする必要があります。この方法は、デフォルト値やその他の起動操作を追加するためのものです。

関連する問題