7

私はコアデータに不慣れで、1つのクエリでさまざまなタイプのすべての子オブジェクトを取得しようとしています。親として「動物」タイプがあり、子として「猫」、「犬」、「鳥」があるとします。猫と犬の両方を取得したいのですが、Animalオブジェクトとして返される単一のクエリでBirdsを取得したくありません。出来ますか?

4

3 に答える 3

5

管理対象オブジェクトにはentityプロパティがあるため、 KevinSylvestreのソリューションの述語と組み合わせることができるはずですentity.name != "Bird"

于 2010-03-12T23:42:16.377 に答える
3

はい、可能です:

// Load delegate from application and context from delegate.
SampleAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = delegate.managedObjectContext;

// Create new request.
NSFetchRequest *request = [[NSFetchRequest alloc] init];

// Create entity description using delegate object context.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Animal" inManagedObjectContext:context];

// Set entity for request.
[request setEntity:entity];
[request setIncludesSubentities:YES];

// Load array of documents.
NSError *error;
NSArray *animals = [context executeFetchRequest:request error:&error];

// Release request.
[request release];

// Access array.
for (id animal in animals) { }
于 2010-03-11T00:48:29.717 に答える
0

これ(entity.name != "Bird")は、「猫」、「犬」、「鳥」しかない場合は機能するかもしれませんが、後で「動物」を追加すると機能しません。使用することもできますentity.name == "Dog" && entity.name == "Cat"

それは「...あなたの場合、他に動物がいるだろうか?」の場合です。

楽しんでください=)

于 2010-03-15T23:31:48.357 に答える