【发布时间】:2011-08-25 15:59:25
【问题描述】:
我正在尝试在核心数据中保留旅行路线,这是我的数据模型。我有实体 Coordinate 将纬度和经度保持为两倍,然后我创建了 Locatable 实体并在这两个实体之间建立关系。
Coordinate <-> Locatable
然后我有 POI 实体继承了 Locatable 这些 POI 代表我的路线。 最后我有 Destination 实体继承了 POI 用作我的目的地点
Coordinate <-> Locatable
|
V
POI
|
V
Destination
我的 Route 类具有与这些 POI 一对多关系的点和与 Destination 一对一的目的地。 当我尝试使用 MKPolylineView 获取我的 POI 集合以创建路线时,会出现问题,当我调用 route.points 时,我还使用这些 POI 获得了我的目的地点。我知道核心数据为 POI 和目的地创建了大的可定位,但这种行为在逻辑上是不正确的,我的目的地点不应该与点一起出现。这是正确的行为还是我遗漏了什么。
如果我错过了一些重要信息,解释起来很复杂,请告诉我。
更新更清晰
我有一对一的路线实体,目的地为目的地,一对多的以 POI 为点(我将其用作我的旅行路径)
当我添加目的地时
route.destination = destination_obj
它也出现在
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 上的错误?
【问题讨论】:
-
什么是 foo? 可定位?
-
我已将 foo 更改为 route 以获得更多描述性。