Ios 为什么CoreData';s fetchDataForEntity是否也获取子实体?
我定义了两个Ios 为什么CoreData';s fetchDataForEntity是否也获取子实体?,ios,core-data,Ios,Core Data,我定义了两个NSManagedObject实体 FTEvent : NSManagedObject 及 当我只获取FTEvent时,令人惊讶的是,我还获取了所有FTFeed实体 NSManagedObjectModel *model; - (NSMutableArray*)fetchDataForEntity:(NSString *)entityName { //Get ALL of a kind of entity from the store NSArray *res
NSManagedObject
实体
FTEvent : NSManagedObject
及
当我只获取FTEvent
时,令人惊讶的是,我还获取了所有FTFeed
实体
NSManagedObjectModel *model;
- (NSMutableArray*)fetchDataForEntity:(NSString *)entityName
{
//Get ALL of a kind of entity from the store
NSArray *result;
NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *rec = [[model entitiesByName] objectForKey:entityName];
[request setEntity:rec];
result = [context executeFetchRequest:request error:&error];
if (!result)
[NSException raise:@"Fetch Failed" format:@"Reason: %@", [error localizedDescription]];
return [[NSMutableArray alloc] initWithArray:result];
}
NSMutableArray allEvents = [self fetchDataForEntity:@"FTEvent"]; // ????
这完全是出于设计:在核心数据中,父子关系的含义与目标C中的继承相同。当您将
FTEvent
设置为FTFeed
的父实体时,您会告诉核心数据每个FTFeed
都是FTEvent
。因此,当您请求所有FTEvent
对象时,也会返回所有“子类”(即“子类”)FTFeed
对象
这个术语让熟悉数据库术语的人感到困惑,因为父子关系意味着组合。请参阅。的“实体继承”部分。如果您不想要子实体,可以在
NSFetchRequest
上将includeSubentities
属性设置为NO
,直到看到您的答案,我正要扔掉我的更改,从头开始。这是解决这个问题的好办法。事实上,我没想到会有这种行为。非常感谢您的澄清。Flexicoders解决方案意味着,我可以继续使用现有的解决方案。@Hooman我不确定您是否可以继续使用现有的解决方案,而无需对其进行修改以适应您正在构建的父子结构。如果在您的模型中,提要是一个事件,那么您可以使用您的结构;如果在您的模型中,甚至有一个或多个提要,您将被迫更改它。
NSManagedObjectModel *model;
- (NSMutableArray*)fetchDataForEntity:(NSString *)entityName
{
//Get ALL of a kind of entity from the store
NSArray *result;
NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *rec = [[model entitiesByName] objectForKey:entityName];
[request setEntity:rec];
result = [context executeFetchRequest:request error:&error];
if (!result)
[NSException raise:@"Fetch Failed" format:@"Reason: %@", [error localizedDescription]];
return [[NSMutableArray alloc] initWithArray:result];
}
NSMutableArray allEvents = [self fetchDataForEntity:@"FTEvent"]; // ????