Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么CoreData';s fetchDataForEntity是否也获取子实体?_Ios_Core Data - Fatal编程技术网

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"];  // ????