从父实体继承的属性的问题 [英] Problem with property inherited from parent entity

查看:110
本文介绍了从父实体继承的属性的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在核心数据中保持旅行路线,这里是我的数据模型。我有实体协调将纬度和经度保持在两倍,然后我创建了可定位实体,并建立这两个实体之间的关系。

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

那么我有 POI 实体继承可定位这些 POI 表示我的路线。
finally我有目标实体继承 POI 作为我的目标点

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

我的路线点与这些 POI 和目标之间的一对多关系与目标一一对应。
当我尝试获取我的 POI 集合为MKPolylineView创建路由时,当我调用route.points我也得到我的目的地点与这些POI时,问题发生。我知道核心数据为POI和目标创建大的Locatable,但是这种行为在逻辑上不正确,我的目标点不应该显示点。这是正确的行为还是我缺少某些东西。

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.

更新更清楚

我有一个以一对一的路由实体,目的地为目的地,一对多POI为点(我用作我的旅行路径)

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)

添加目的地时

route.destination = destination_obj

它也会显示在

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 ?

添加了小数据模型和代码示例,以便每个人都能重现此
Make 3实体

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

以下是我的代码

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];

简而言之,我创建了一个家庭,并添加一个孩子和一个家长到这个家庭, b $ b在第一次运行我得到这个结果

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

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

child entity is included in parents property. This is some kind of my misconception or this is a bug on SDK ?

推荐答案

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

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.

这里是我的建议:

有一个实体位置,属性纬度经度名称。将纬度经度设置为非可选,但将名称设置为可选。

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.

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

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.

现在,如果您想存储特定的旅行路线,您可以引入实体路线,其中包含名称日期等,以及与位置之间的一对多关系。请注意,您无法将许多地理位置置于此计划的任何特定顺序。但是,您可以做的是向位置添加两个一对一关系,一个称为 startLocation ,一个目标

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.

如果您真的需要一个更复杂的核心数据模型中的行程描述,您可能需要另一个实体。您可以将旅行点称为旅行点,并与路线建立多对一关系,与位置 strong> arrivalDate , timeStayed departureDate 顺序

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.

这篇关于从父实体继承的属性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆