2012-01-24 9 views
0

親/子関係の子であるエンティティにNewObjectを挿入するときに問題があります。これは、ローカルSQLiteベースのCoreDataアプリケーションです。メインウィンドウには2つのエンティティが2つのTableViewで表示されます。 contentSetを使用すると、子の表には選択した親に関するデータのみが表示されます。シートから親/子関係のinsertNewObjectにデータを渡す

子にデータを追加するには、3番目のエンティティからの項目テーブルをシートに表示します。ユーザーはこのテーブルから選択し、[追加]をクリックする必要があります。シートを閉じると、メインウィンドウの子テーブルは新しい行で更新されます。問題:何も表示されません。

サードパーティのアプリケーションでデータベースの内容を確認すると、新しいデータがそこにあることがわかりますが、親との関係に関する情報が保存されていないためテーブルビューに表示されませんそれが親に関係する。

私のコードにはこれに関する情報がありませんが、私はこれをどのようにプログラムするべきか分かりません。言い換えれば、シートを解体するときに、選択された親がどれかを特定し、子に新しいデータを挿入するときにこの関係情報を指定します。助けていただければ幸いです。あなたが作成したCollectedItemsオブジェクトとその親Collectorsオブジェクト間の関係を追加する必要が

// there are 3 entities: Collectors (parent), CollectedItems (child) and Items. 

    // we call the sheet presenting the Items list to pick from 
    - (IBAction)showAddItemDialog:(id)sender { 
    [NSApp beginSheet:addItemDialog 
modalForWindow:window 
modalDelegate:self 
    didEndSelector:@selector(didEndAddItemSheet:returnCode:contextInfo:) 
    contextInfo:nil]; 
    } 

    // we dismiss the sheet by clicking on Cancel or Add 
    - (IBAction)dismissAddItemDialog:(id)sender { 
[NSApp endSheet:addItemDialog returnCode:([sender tag])]; 
    [addItemDialog orderOut:sender]; 
    } 

    // depending on clicked button, do nothing or pass selected data 
    - (void)didEndAddItemSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo (void *)contextInfo { 
    if (returnCode == 0) { 
    // do nothing, this is the effect of clicking on Cancel 
    } 
    if (returnCode == 1) { 
    NSString *theName = [[[itemsPickerController selectedObjects] valueForKey:@"itemName"] objectAtIndex:0]; 
    // above, we get the value from the itemName attribute sleected in the Items list 
    NSLog (@"%@", theName); 
    // the variable is displayed in the console, so it was correctly selected 
    [self addNewItemWithName:theName]; 
    } 
    } 

    // we use the passed data to create new object (row) in the CollectedItems entity 
    - (id)addNewItemWithName:(NSString *)theName { 
    NSEntityDescription *newContent = [NSEntityDescription insertNewObjectForEntityForName:@"CollectedItems" inManagedObjectContext:[self managedObjectContext]]; 
    [newContent setValue:theName forKey:@"collectedItemName"]; 
    // above, we insert a new row in CollectedItems, assigning the value theName to the attribute collectedItemName 
    return nil; 
    } 

答えて

1

は、ここに私のコードです。 Collectorsにコアデータユーティリティメソッドがあります(コアデータ管理オブジェクトクラスを生成している場合)。それはaddCollectedItemsObjectのように呼ばれます。

テーブルビューを再読み込みすると、正しいデータで更新する必要があります。これで、現在の関係が分かります。

さらに、NSFetchedResultsControllerを使用してテーブル内のデータを制御すると、データモデルを更新するときにテーブルに自動的に変更が反映されます。

+0

ご返信ありがとうございます。実際には、あなたが言及したメソッドを含む管理対象オブジェクトのサブクラスを作成しましたが、上のコードにそれらをどのように「接続」するかを実際に理解していません。また、(id)newObjectメソッドを含むNSArrayControllerをサブクラス化してデータを追加するクラスもあります。私はシートメソッドを作成する前にテスト用に使っていましたが、それは正しく動作していました(関係を処理する場合)。 –

+0

コアデータ(NSFetchRequest)から関連する 'Collectors'オブジェクトを最初にフェッチする必要があります。その後、' CollectedItems'をコアデータに挿入した後、Collectorでそのメソッドを使用して関連付けます。この関係をストアに保持するには、マネージオブジェクトコンテキストでsave:を呼び出します。あなたのアーキテクチャがどのように見えるかわからないですが、例えばaddNewItemWithName:メソッドはaddNewItemWithName:(NSString *)という名前をtoCollectors :(コレクター*)コレクター....またはそのようなものにすることができます。 – bandejapaisa

+0

ありがとう、私はいくつかのソリューションを試しています。 –

0

と記録のために、それが役立つかもしれないそれらのために、ここでは、最終的な作業コードは次のとおりです。メソッドを確保

は(管理コアデータオブジェクトクラス)のための対応コレクタークラスファイルに生成されました。そして

// header file 
@class Collecteditems; 
@interface Collectors : NSManagedObject { } 
@property (nonatomic, retain) NSSet* content; 
@end 
@interface Collectors (CoreDataGeneratedAccessors) 
- (void)addContentObject:(Collecteditems *)value; 
@end 

// implementation file 
#import "Collectors.h" 
#import "Collecteditems.h" 
@implementation Collectors 
@dynamic content; 
- (void)addContentObject:(Collecteditems *)value { 
NSSet *s = [NSSet setWithObject:value]; 
[self willChangeValueForKey:@"collecteditems" withSetMutation:NSKeyValueUnionSetMutation usingObjects:s]; 
[[self primitiveValueForKey:@"collecteditems"] addObject:value]; 
[self didChangeValueForKey:@"collecteditems" withSetMutation:NSKeyValueMinusSetMutation usingObjects:s]; 
} 

(参考のために、元のポスト内のIDを参照)NSArrayControllerは、収集アイテムテーブルを制御する上でAddObjectメソッドを追加します。

- (id)addNewItemWithName:(NSString *)theName { 
    NSEntityDescription *newContent = [NSEntityDescription insertNewObjectForEntityForName:@"CollectedItems" inManagedObjectContext:[self managedObjectContext]]; 
    [newContent setValue:theName forKey:@"collectedItemName"]; 
    [collectedItemsController addObject:newContent]; // line added 
    return nil; 
    } 
関連する問題