7

我是核心数据的新手,并尝试通过一个查询获取各种类型的所有子对象。假设有一个“动物”类型作为父母,“猫”、“狗”和“鸟”作为孩子。我想同时获得猫和狗,但不是作为 Animal 对象返回的单个查询中的 Birds。可能吗?

4

3 回答 3

5

托管对象具有entity属性,因此您应该能够将Kevin Sylvestre 的解决方案与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

如果您只有“Cat”、“Dog”和“Bird”,这个 ( entity.name != "Bird") 可能会起作用,但如果您稍后添加更多“Animals”,它就不起作用了。你也可以使用entity.name == "Dog" && entity.name == "Cat"

这是一个“......你还有其他的吗,在你的情况下,动物?”

玩得开心 =)

于 2010-03-15T23:31:48.357 回答