从父实体继承的属性问题
我试图在核心数据中保留旅行路线,这是我的数据模型。我有实体坐标,将纬度和经度保持为双精度,然后创建可定位实体并在这两个实体之间建立关系。
Coordinate <-> Locatable
然后我有 POI 实体继承了 Locatable 这些 POI 代表我的路线。 最后我有 Destination 实体继承 POI 用作我的目的地点
Coordinate <-> Locatable
|
V
POI
|
V
Destination
我的 Route 类具有与这些 一对多关系的点POI 和目的地与目的地 是一对一的。 当我尝试获取我的 POI 集合以使用 MKPolylineView 创建路线时,会出现问题,当我调用 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 上的错误?
I'm trying to keep traveling route in core data and here is my data model. I have entity Coordinate keeping latitude and longitude in double, then I created Locatable entity and make relation between these two entities.
Coordinate <-> Locatable
then I have POI entity inherited Locatable these POI representing my route.
finally I have Destination entity inherited POI using as my destination point
Coordinate <-> Locatable
|
V
POI
|
V
Destination
My Route class have points as one-to-many relation with these POI and destination as one-to-one with Destination.
Problem occur when I try to get my POI collection for create route with MKPolylineView, when I call route.points I also get my destination point with those POI. I know core data create big Locatable for both POI and Destination, but this behavior isn't logically right my destination point shouldn't show up with points. Is this right behavior or I missing something.
Quite complicated to explain if I miss some important information please tell me.
Updated for more clarify
I have Route entity with one-to-one with Destination as destination and one-to-many with POI as points(which I using as my traveling path)
When I add destination
route.destination = destination_obj
it also show up in
route.points
which doesn't right because these two property serve two different purpose (one for making traveling path, another for pre calculate some data). Is this behavior have any document or explanation ?
Added small data model and code sample so every one can reproduce this
Make 3 entity
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
Here is my code
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];
in short I create family and add one child and one parent to this family and print out some output
in the first run I got this result
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
which is what I expected, then I rerun the app again and this what the weird thing occur
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
child entity is included in parents property. This is some kind of my misconception or this is a bug on SDK ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的设置太复杂了。此外,您设置的关系有点像子类。但是关系和子类是完全不同的概念,我认为您可能会混淆它们。
这是我的建议:
拥有一个实体位置,其属性为纬度、经度和名称。将纬度和经度设置为非可选,但将名称设置为可选。现在您已拥有所需的所有基本构建块。
在代码中,您可以定义对象,例如 *Location 类型的 POI、*Location 类型的目的地和 *Location 类型的 startPoint。例如,您可以使用坐标来计算最短路线。通过设置名称,您可以将任何位置设为 POI。如果您需要非 POI 的名称,请创建布尔属性 isPOI。
现在,如果你想存储特定的旅行路线,你可以引入一个实体 Route,其属性包括 name、date 等属性和一个与位置的多对关系。请注意,您不能使用此方案将许多位置按任何特定顺序排列。但是,您可以做的是再添加两个与 Location 的一对一关系,一个称为 startLocation,另一个称为 destination。
如果您确实需要在核心数据模型中对行程进行更复杂的描述,则可能需要另一个实体。您可以将其称为 TravelPoints,与 Route 具有多对一关系,与 Location 具有一对一关系,以及诸如 Location 之类的属性strong>arrivalDate、timeStayed 或 departureDate、sequentialNumber(表示Route中的顺序)、等等。
通过此设置你应该能够让你的项目走得更远。
I think your setup is too complicated. Also, you set up your relationships a bit like sub-classes. But relationships and sub-classes are completely different concepts, and I think you might be mixing them up.
Here is what I propose:
Have an entity Location, with attributes latitude, longitude and name. Set latitude and longitude as non-optional, but name as optional. Now you have all the basic building blocks you need.
In your code you can define objects such as *POI of type Location, *destination of type Location and *startPoint of type Location. You can use the coordinates to calculate the shortest route for example. By setting the name you can make any Location into a POI. If you want names for non-POIs, create a boolean attribute isPOI.
Now, if you want to store specific travel routes, you can introduce an entity Route, with attributes like name, date etc. and a one-to-many relationship to Location. Note that you can not put the many Locations in any particular order with this scheme. However, what you could do is include two more one-to-one relationships to Location, one called startLocation and one destination.
If you really need a more complex description of itineraries in your core data model, you probably need another entity. You could call it TravelPoints with a many-to-one relationship to Route, a one-to-one relationship to Location and attributes like arrivalDate, timeStayed or departureDate, sequentialNumber (to indicated the order in Route), etc.
With this setup you should be able to go far with your project.