在Core Data中创建动态模式? [英] Faking a dynamic schema in Core Data?

查看:137
本文介绍了在Core Data中创建动态模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过阅读Core Data上的Apple文档,我了解到当您需要动态模式时,您不应该使用Core Data。如果我想为用户提供创建自己的属性的能力,在核心数据模型中,如果我创建了一些虚拟属性,如自定义十进制1,自定义十进制2,自定义文本1 自定义文本2等,用户可以命名和使用为自己的目的?

From reading the Apple Docs on Core Data, I've learned that you should not use Core Data when you need a dynamic schema. If I wanted to provide the user the ability to create their own properties, in a core data model would it work if I created some "dummy" attributes like "custom decimal 1", "custom decimal 2", "custom text 1", "custom text 2" etc that the user could name and use for their own purposes?

显然,这将不适用于关系,但对于简单的属性,它似乎是一个合理的解决方法。在我的实体上创建一堆虚拟属性,大多数用户未使用会明显降低它们的性能?你有任何人试过这样的东西吗?感谢!

Obviously this won't work for relationships, but for simple properties it seems like a reasonable workaround. Will creating a bunch of dummy attributes on my entities that go unused by most users noticeably decrease performance for them? Have any of you tried something like this? Thanks!

推荐答案

首先,请参阅核心数据文档关系。使用您的示例,考虑如下:

First, see the Core Data docs on relationships. Using your example, consider something like:


  1. 一个CarAttributeType实体,名称为weight in pounds

  2. 一个CarAttribute实体,其值为2765.

  3. 一个Car实体,具有您提到的所需值(如color,make

然后,在CarAttribute和CarAttributeType之间有多对一的关系(许多CarAttribute可以有相同的类型) Car和CarAttribute之间的多对多关系(每辆车可以有很多属性)。此解决方案比硬编码的NULL字段设置要复杂一些。但是,它避免了重复群组,希望更容易维护。

Then, have a many-to-one relationship between CarAttribute and CarAttributeType (many CarAttributes can have the same type), a one-to-many relationship between Car and CarAttribute (each car can have many attributes). This solution is a bit more complicated to setup than the hard-coded NULL fields. However, it avoids repeating groups and is hopefully more maintainable.

编辑:是的,我错过了。我想你会想要一个StringCarAttribute,StringCarAttributeType,FloatCarAttribute,FloatCarAttributeType等。然后,在StringCarAttribute和StringCarAttributeType之间有一个多对一,等等。Car将有一个到manys与StringCarAttribute和FloatCarAttribute。多个类型实体的原因是你没有一个StringCarAttribute和FloatCarAttribute,都声明自己使用一个权重属性类型。

Yes, I missed that. I think you would want a StringCarAttribute, StringCarAttributeType, FloatCarAttribute, FloatCarAttributeType, etc. Then, have a many-to-one between StringCarAttribute and StringCarAttributeType, etc. Car will have one-to-manys with both StringCarAttribute and FloatCarAttribute. The reason for multiple type entities is so you don't have a StringCarAttribute and FloatCarAttribute, both declaring themselves to be using a single weight attribute type.

有一个CarAttribute与所有类型违反1NF#4。

Having one CarAttribute with all the types goes against 1NF #4.

这篇关于在Core Data中创建动态模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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