Core data 从父实体继承的属性存在问题

Core data 从父实体继承的属性存在问题,core-data,entity,parent,Core Data,Entity,Parent,我试图在核心数据中保留旅行路线,这是我的数据模型。我让实体坐标保持纬度和经度的双精度,然后我创建了可定位实体,并在这两个实体之间建立了关系 Coordinate <-> Locatable 它也出现在 route.points 这是不对的,因为这两个属性有两个不同的用途(一个用于生成旅行路径,另一个用于预计算某些数据)。这种行为是否有任何文件或解释 添加了小数据模型和代码示例,这样每个人都可以复制这个 组成3个实体 Family - int generation - paren

我试图在核心数据中保留旅行路线,这是我的数据模型。我让实体坐标保持纬度和经度的双精度,然后我创建了可定位实体,并在这两个实体之间建立了关系

Coordinate <-> Locatable
它也出现在

route.points
这是不对的,因为这两个属性有两个不同的用途(一个用于生成旅行路径,另一个用于预计算某些数据)。这种行为是否有任何文件或解释

添加了小数据模型和代码示例,这样每个人都可以复制这个 组成3个实体

Family 
- int generation
- parents as to-may relation to Parent and family as inverse
- child as to-one relation to Child and family as inverse
Parent
- String name
- family to-one relation to Family
Child : Parent
这是我的密码

Family *fam;

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Family" inManagedObjectContext:self.managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];

NSArray *meters = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];

if ([meters count] > 0) {
    NSLog(@"found");
    fam = [meters lastObject];
    fam.generation = [NSNumber numberWithInt:[fam.generation intValue] + 1];
} else {
    NSLog(@"new");
    fam = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:self.managedObjectContext];
    fam.generation = [NSNumber numberWithInt:1];
    [self saveContext];
};
NSLog(@"There are %d paren", [fam.parents count]);
for (Parent *p in fam.parents) {
    NSLog(@"name : %@", p.name);
}
Child *child;
if (!fam.child) {
    child = [NSEntityDescription insertNewObjectForEntityForName:
             [[NSEntityDescription entityForName:@"Child" inManagedObjectContext:self.managedObjectContext] name]
                                          inManagedObjectContext:self.managedObjectContext];
    fam.child = child;
}
fam.child.name = [NSString stringWithFormat:@"child number %d", [fam.generation intValue]];
NSLog(@"There are %d parent after adding one child", [fam.parents count]);

Parent *parent = [NSEntityDescription insertNewObjectForEntityForName:
                  [[NSEntityDescription entityForName:@"Parent" inManagedObjectContext:self.managedObjectContext] name]
                                               inManagedObjectContext:self.managedObjectContext];
parent.name = [NSString stringWithFormat:@"parent number %d", [fam.generation intValue]];
[fam addParentsObject:parent];

NSLog(@"There are  %d parent after add parent", [fam.parents count]);
for (Parent *p in fam.parents) {
    NSLog(@"name : %@", p.name);
}

[self saveContext];
简言之,我创建了一个族,并将一个子族和一个父族添加到此族中,然后打印出一些输出 在第一轮比赛中,我得到了这个结果

2011-08-27 19:06:28.271 child[2015:207] new
2011-08-27 19:06:28.276 child[2015:207] There are 0 paren
2011-08-27 19:06:28.278 child[2015:207] There are 0 parent after adding one child
2011-08-27 19:06:28.279 child[2015:207] There are  1 parent after add parent
2011-08-27 19:06:28.280 child[2015:207] name : parent number 1
这就是我所期望的,然后我再次运行应用程序,这就是奇怪的事情发生的原因

2011-08-27 19:08:12.383 child[2035:207] found
2011-08-27 19:08:12.386 child[2035:207] There are 2 paren
2011-08-27 19:08:12.387 child[2035:207] name : parent number 1
2011-08-27 19:08:12.388 child[2035:207] name : child number 1
2011-08-27 19:08:12.389 child[2035:207] There are 2 parent after adding one child
2011-08-27 19:08:12.390 child[2035:207] There are  3 parent after add parent
2011-08-27 19:08:12.390 child[2035:207] name : parent number 1
2011-08-27 19:08:12.391 child[2035:207] name : parent number 2
2011-08-27 19:08:12.391 child[2035:207] name : child number 2

子实体包含在父属性中。这是我的一种误解,或者这是SDK上的一个bug?

我认为您的设置太复杂了。此外,您设置的关系有点像子类。但是关系和子类是完全不同的概念,我想你可能把它们混淆了

以下是我的建议:

有一个实体位置,具有属性纬度经度名称。将纬度经度设置为非可选,但将名称设置为可选。现在,您已经拥有了所需的所有基本构建块

在代码中,您可以定义对象,例如位置类型的*POI位置类型的*destination位置类型的*startPoint。例如,可以使用坐标计算最短路线。通过设置名称,可以将任何位置设置为POI。如果需要非POI的名称,请创建一个布尔属性isPOI

现在,如果您想存储特定的旅行路线,您可以引入一个实体Route,该实体具有namedate等属性,并与Location建立一对多关系。请注意,不能使用此方案按任何特定顺序放置多个位置。但是,您可以做的是包括与位置的另外两个一对一关系,一个称为定位和一个目的地

如果您确实需要在核心数据模型中对行程进行更复杂的描述,那么您可能需要另一个实体。您可以将其称为旅行点,它与路线、地点、到达日期等属性有多对一的关系(指示路线中的顺序)等


通过此设置,您应该能够在项目中取得更大进展。

什么是foo?一个可定位?我已将foo更改为route以获得更详细的说明。核心数据中的子类与实际子类有何不同?问题是,当我设置一个属性(目标)时,它与另一个属性(POI)混淆了你有关于这种情况的任何文档或参考资料吗,谢谢。一个真正的子类从它的超类继承属性并扩展这些属性或覆盖默认属性。当你将一个位置定义为路由的目的地并且该位置也是一个POI时,你只需要一个附加的信息,你可以使用,也可以不使用。所以我这样做了看不到任何问题。--通常,如果您可以将信息封装在一个实体中,您希望避免太多“类似子类”的一对一关系。性能问题是一回事,但现在我在属性上遇到了问题(调用route.points也返回destination)。然后不要将目的地放在route.points中,将其放在route.destination中。实体越少,性能应该越好。是的,我按route.destination=destination放置目的地,但当我调用route.points时,目的地也显示在那里。这让我非常困惑。
2011-08-27 19:06:28.271 child[2015:207] new
2011-08-27 19:06:28.276 child[2015:207] There are 0 paren
2011-08-27 19:06:28.278 child[2015:207] There are 0 parent after adding one child
2011-08-27 19:06:28.279 child[2015:207] There are  1 parent after add parent
2011-08-27 19:06:28.280 child[2015:207] name : parent number 1
2011-08-27 19:08:12.383 child[2035:207] found
2011-08-27 19:08:12.386 child[2035:207] There are 2 paren
2011-08-27 19:08:12.387 child[2035:207] name : parent number 1
2011-08-27 19:08:12.388 child[2035:207] name : child number 1
2011-08-27 19:08:12.389 child[2035:207] There are 2 parent after adding one child
2011-08-27 19:08:12.390 child[2035:207] There are  3 parent after add parent
2011-08-27 19:08:12.390 child[2035:207] name : parent number 1
2011-08-27 19:08:12.391 child[2035:207] name : parent number 2
2011-08-27 19:08:12.391 child[2035:207] name : child number 2