xcode 使用核心数据进行存储 - 在基于导航和基于窗口的应用程序中 - iPhone

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1343907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:45:51  来源:igfitidea点击:

Use Core Data for Storage - in navigation based & window based application - iPhone

iphonexcodeuinavigationcontroller

提问by Sagar R. Kothari

There are too many options for creating projects in XCode,

在 XCode 中创建项目的选项太多了,

But When we select Navigation Based Application / Window based Application

但是当我们选择基于导航的应用程序/基于窗口的应用程序时

We can see the extra option - Use Core Data For Storage.

我们可以看到额外的选项 - 使用核心数据进行存储。

I need brief detail about it.

我需要关于它的简要细节。

What's new in it?

它有什么新东西?

回答by Hyman Cox

To elaborate on what Jergason wrote. Core Data is an Object-Relational Mapping (ORM) similar to Hibernate in the Java world. It abstracts the actual mechanics of storing data (such as SQL or .plist files) away from your code. Your code just needs to deal with a consistent object oriented framework to fetch objects, update them and persist them. Core Data supports some level of ACID transactions, but not 2-Phase commits. On the iPhone, the default settings for Core Data wrap the sqlite databases with the ORM layer.

详细说明 Jergason 所写的内容。Core Data 是一种类似于 Java 世界中的 Hibernate 的对象关系映射 (ORM)。它从您的代码中抽象出存储数据(例如 SQL 或 .plist 文件)的实际机制。您的代码只需要处理一个一致的面向对象框架来获取对象、更新它们并持久化它们。Core Data 支持某种级别的 ACID 事务,但不支持 2-Phase 提交。在 iPhone 上,Core Data 的默认设置用 ORM 层包装 sqlite 数据库。

One of the interesting side benefits of using Core Data is the tool to visually design your data model and to generate the model classes. If you have a large model this can save alot of time in hand coding model classes.

使用 Core Data 的一个有趣的附带好处是可视化设计数据模型和生成模型类的工具。如果您有一个大型模型,这可以节省大量手动编码模型类的时间。

Another interesting benefit of Core Data is it's ability to migrate your database from one model version to another. This is very important in the iPhone world since you may want to modify your data model from one version of your app to the next. Core Data provides a pretty straightforward way to migrate the persisted data from the old model to the new without you having to write a ton of migration code. You just define a migration map and add a 'few' lines of code to your app delegate and things get converted for you.

Core Data 的另一个有趣的好处是它能够将您的数据库从一个模型版本迁移到另一个模型版本。这在 iPhone 世界中非常重要,因为您可能希望将数据模型从应用程序的一个版本修改为下一个版本。Core Data 提供了一种非常简单的方法来将持久数据从旧模型迁移到新模型,而您无需编写大量迁移代码。您只需定义一个迁移映射并向您的应用程序委托添加“几行”代码,事情就会为您转换。

Core Data on the iPhone is designed for the mobile environment. If you fetch all the rows in a table into an array it doesn't actually pull everything into memory. It creates what Apple calls a faulting array which is just an object that looks like an NSArray. When you access the various elements of the array Core Data fetches those entities (rows) on use, not on query. It saves memory and helps your app run faster.

iPhone 上的 Core Data 是为移动环境设计的。如果您将表中的所有行提取到数组中,它实际上并没有将所有内容都拉入内存。它创建了 Apple 所说的错误数组,它只是一个看起来像 NSArray 的对象。当您访问数组的各种元素时,Core Data 会在使用而不是查询时获取这些实体(行)。它可以节省内存并帮助您的应用程序运行得更快。

All-in-all it's a pretty full featured ORM layer, not as rich as Hibernate, but sufficient for this environment.

总而言之,它是一个功能相当齐全的 ORM 层,不像 Hibernate 那样丰富,但对于这种环境来说已经足够了。

回答by jergason

Core Data is essentially a way to build your model visually, a sort of Interface Builder for the model. You create entities that represent model classes, and define relationships between them. Instead of having to code up a Person class that has an instance of an address class, you can just create them visually by dragging and dropping. There is lots more to it than that, but that is a main feature that I think is cool.

Core Data 本质上是一种可视化构建模型的方法,是模型的一种 Interface Builder。您创建表示模型类的实体,并定义它们之间的关系。不必编写具有地址类实例的 Person 类,您只需通过拖放即可直观地创建它们。它还有很多其他功能,但这是我认为很酷的主要功能。

Also see thisApple documentation.

另请参阅 Apple 文档。