iPhone:如何处理核心数据关系
总的来说,我对核心数据和数据库很陌生。现在我需要解析 XML 并将内容存储在 Core Data 中。 XML 看起来像这样:
<books>
<book id="123A" name="My Book">
<page id="95D" name="Introduction">
<text id="69F" type="header" value="author text"/>
<text id="67F" type="footer" value="author text"/>
</page>
<page id="76F" name="Chapter 1">
<text id="118" type="selection">
<value data="1">value1</value>
<value data="2">value2</value>
<value data="3">value3</value>
</text>
</page>
</book>
<book id="124A"...
根据我的理解,我需要四个实体,例如“书籍”、“书籍”、“页面”和“文本”。我想知道如何正确设置关系以及如何将 Page 对象添加到 Book 对象以及如何检索 Text 对象属性的值?我发现的教程主要涉及一个实体,所以我并没有真正理解这个想法..非常感谢您的帮助!
I'm new to Core Data and databases in general. Now I need to parse a XML and store the contents in Core Data. The XML looks something like this:
<books>
<book id="123A" name="My Book">
<page id="95D" name="Introduction">
<text id="69F" type="header" value="author text"/>
<text id="67F" type="footer" value="author text"/>
</page>
<page id="76F" name="Chapter 1">
<text id="118" type="selection">
<value data="1">value1</value>
<value data="2">value2</value>
<value data="3">value3</value>
</text>
</page>
</book>
<book id="124A"...
From my understanding I would need four Entities, like "Books", "Book", "Pages" and "Text". I wonder how to set the relationships correctly and how to add for example a Page object to a Book object and how to retrieve a Text object attribute's value? The tutorials I have found mostly deal with one Entity so I didn't really get the idea.. Gtrateful for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,您需要三个实体。您可以将“Books”视为您正在使用的 CoreData 数据库。 CoreData 数据库包含许多称为书籍的实体。
我认为您拥有的数据模型有点奇怪,但我想这对您的应用程序来说是有意义的。要将其映射到 CoreData,我将:
添加实体 Book、Page、Text
分别向它们添加 bookId、pageId、textId。
然后添加从 Page 到 Book 以及从 Text 到 Page 的关系。
到那时,您应该能够通过询问所有具有
Book = 您感兴趣的图书
的页面来打印整本书,然后按其 pageId 对所有这些页面进行排序>
并按顺序询问所有包含以下内容的文本
页面 = 当前页面
,然后按textId对这些文本进行排序。
可能出现的问题是 Text 可以有多个值,如上面的 XML 所示。您可以通过添加另一个名为 Value 的实体来使用它,但我可能会通过直接向 Text 实体添加属性“value”和“type”来解决它。 (然后,您可以在打印页面时使用“值”作为第二个排序键。
No, you'd need three entities. You can think of "Books" as the CoreData database you're using. The CoreData database then includes a number of entities called book.
I think the data model you have is a bit weird, but I guess it makes sense for your application. To map it to CoreData I would:
Add the entities Book, Page, Text
Add a bookId, pageId, textId to them, respectively.
Then add a relation from Page to Book, and from Text to Page.
By then you should be able to print out a whole book by asking for all Pages that have
Book = the book you're interested in
and then order all those Pages by their pageId
and in order, ask for all texts that have
Page = the current page
then order those Texts by their textId.
What might be a problem is that a Text can have multiple Values, as seen in your XML above. You could use this by adding another entity called Value, but I would probably solve it by adding the attributes "value" and "type" to the Text entity directly. (You could then use "value" as a second sort key when printing out a page.
查看这些链接:
http://developer.apple.com/iphone/library/documentation/可可/概念/CoreData/
http://developer.apple.com/cocoa/coredatatutorial/index.html(对于常规 Cocoa,但相同的原则成立,所以这应该有所帮助)
Check out these links:
http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreData/
http://developer.apple.com/cocoa/coredatatutorial/index.html (for regular Cocoa, but the same principles hold so this should help)