0

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

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

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

私のコードにはこれに関する情報がありませんが、これをどのようにプログラムすればよいかわかりません。つまり、シートを閉じるときに、選択されている親を特定し、子に新しいデータを挿入するときにこの関係情報を指定します。助けていただければ幸いです。

これが私のコードです:

  // 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;
  }
4

2 に答える 2

1

CollectedItems作成したオブジェクトとその親オブジェクトの間の関係を追加する必要がありCollectorsます。Collectorsコア データ ユーティリティ メソッドが含まれます (コア データ管理オブジェクト クラスを生成した場合)。のような名前になりますaddCollectedItemsObject

tableViews をリロードすると、正しいデータで更新されます。これで関係が認識されるようになりました。

さらに良いのは、NSFetchedResultsController を使用してテーブル内のデータを制御し、データ モデルを更新したときにテーブルが自動的に変更を反映するようにすることです。

于 2012-01-24T17:34:24.777 に答える
0

記録のために、それが役立つかもしれない人のために、これが最終的な作業コードです:

(コア データ管理対象オブジェクト クラス) の対応する Collectors クラス ファイルでメソッドが生成されたことを確認します。

// 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];
}

次に、収集された項目のテーブルを制御する NSArrayController に addObject メソッドを追加します (参照については、元の投稿の id を参照してください)。

- (id)addNewItemWithName:(NSString *)theName {
    NSEntityDescription *newContent = [NSEntityDescription insertNewObjectForEntityForName:@"CollectedItems" inManagedObjectContext:[self managedObjectContext]];
    [newContent setValue:theName forKey:@"collectedItemName"];
    [collectedItemsController addObject:newContent]; // line added
    return nil;
  }
于 2012-02-06T16:13:11.830 に答える