开发者

Core Data: Can I add a relationship to the parent entity?

I made the Photo entity, which has a Data attribute to store the image data. Photo is the parent of the Thumbnai开发者_高级运维l entity. I also added a one-to-one relationship between Photo and Thumbnail so that a Photo can have a Thumbnail.

This seems to work but is sort of confusing.

Do you think it's better design to make another entity called Image that has the image data attribute and make Photo & Thumbnail children of Image?


Do you think it's better design to make another entity called Image that has the image data attribute and make Photo & Thumbnail children of Image?

You shouldn't look at entity inheritance as a means of making a tidy design but instead it should be used such that multiple entities can inhabit the same relationship.

Suppose you had a Person entity that had image data but the images where of different sizes or types but you needed all the image entities to be in the same relationship. You would set things up like so:

Person{
    images<-->>Image.person
}

Image{
    person<<-->Person.images
}

GIF:Image{
    //... GIF related attributes
}

JPG:Image{
    //... JPG related attributes
}

Because the GIF and JPG entities inherit from Image you can put both in the Person.images relationship.

In your case, there appears to be no particular reason, other than analogy to Classes, to make the Photo and Thumbnail inherit from a common super entity.

Also, if you are using an SQLite store, all the subentities of a parent entity end up in the same table. For very large data sets, this can create performance problems. So, that is another reason not to use entity inheritance just for neatness.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜