核心数据与 SQLite 3

2021-12-06 00:00:00 macos sqlite core-data cocoa

我对关系数据库已经很熟悉了,并且已经使用过SQLite(和其他数据库)过去.不过,Core Data 有一定的吸引力,所以考虑花点时间学习一下使用在我的下一个申请中.

I am already quite familiar with relational databases and have used SQLite (and other databases) in the past. However, Core Data has a certain allure, so I am considering spending some time to learn it for use in my next application.

与 SQLite 相比使用 Core Data 是否有很多好处,反之亦然?各自的优缺点是什么?

Is there much benefit to using Core Data over SQLite, or vice versa? What are the pros/cons of each?

当 Apple 没有将 Core Data 用于其许多旗舰应用程序(例如 Mail.app 或 iPhoto.app)而是选择 SQLite 数据库时,我发现很难证明学习 Core Data 的成本是合理的.SQLite 也广泛用于 iPhone.

I find it hard to justify the cost of learning Core Data when Apple doesn't use it for many of its flagship applications like Mail.app or iPhoto.app - instead opting for SQLite databases. SQLite is also used extensively on the iPhone.

熟悉使用这两种方法的人能否评价一下他们的体验?或许,与大多数事情一样,这个问题比仅使用一个比另一个更深?

Can those familiar with using both comment on their experience? Perhaps, as with most things, the question is deeper than just using one over the other?

推荐答案

虽然 Core Data 是 Apple 的后代Enterprise Object Framework,一个对象关系映射器 (ORM),与关系后端紧密相连,Core Data 不是一个 ORM.事实上,它是一个对象图管理框架.它管理一个潜在的非常大的对象实例图,允许应用程序通过根据需要在内存中和内存外故障对象来处理不完全适合内存的图.Core Data 还管理对属性和关系的约束并维护引用完整性(例如,当对象被添加到/从关系中删除/从关系中删除时,保持前向和后向链接一致).因此,Core Data 是构建 MVC 架构的模型"组件的理想框架.

Although Core Data is a descendant of Apple's Enterprise Object Framework, an object-relational mapper (ORM) that was/is tightly tied to a relational backend, Core Data is not an ORM. It is, in fact, an object graph management framework. It manages a potentially very large graph of object instances, allowing an app to work with a graph that would not entirely fit into memory by faulting objects in and out of memory as necessary. Core Data also manages constraints on properties and relationships and maintains reference integrity (e.g. keeping forward and backward links consistent when objects are added/removed to/from a relationship). Core Data is thus an ideal framework for building the "model" component of an MVC architecture.

为了实现图形管理,Core Data 碰巧使用 SQLite 作为磁盘存储.它可以使用不同的关系数据库甚至非关系数据库(例如CouchDB<)实现/a>.正如其他人指出的那样,Core Data 也可以使用 XML 或二进制格式或用户编写的原子格式作为后端(尽管这些选项要求整个对象图适合内存).如果您对如何在 SQLite 后端实现 Core Data 感兴趣,您可能需要查看 OmniGroup 的 OmniDataObjects 框架,一个核心数据 API 子集的开源实现.BaseTen 框架也是使用 PostgreSQL 作为后端的 Core Data API 的实现.

To implement its graph management, Core Data happens to use SQLite as a disk store. It could have been implemented using a different relational database or even a non-relational database such as CouchDB. As others have pointed out, Core Data can also use XML or a binary format or a user-written atomic format as a backend (though these options require that the entire object graph fit into memory). If you're interested in how Core Data is implemented on an SQLite backend, you might want to check out OmniGroup's OmniDataObjects framework, an open source implementation of a subset of the Core Data API. The BaseTen framework is also an implementation of the Core Data API using PostgreSQL as a backend.

因为 Core Data 不是 SQLite 的 ORM,它不能读取任意 SQLite 模式.相反,您不应该依赖于能够使用其他 SQLite 工具读取 Core Data 的 SQLite 数据存储;架构是一个可能会改变的实现细节.

Because Core Data is not intended to be an ORM for SQLite, it cannot read arbitrary SQLite schema. Conversely, you should not rely on being able to read Core Data's SQLite data stores with other SQLite tools; the schema is an implementation detail that may change.

因此,直接使用 Core Data 或 SQLite 之间并没有真正的冲突.如果您需要关系数据库,请使用 SQLite(直接或通过 Objective-C 包装器之一,例如 FMDB) 或关系数据库服务器.但是,您可能仍想学习 Core Data 以用作对象图管理框架.结合 Apple 的控制器类和键值绑定兼容的视图小部件,您可以用非常的代码实现完整的 MVC 架构.

Thus, there is not really any conflict between using Core Data or SQLite directly. If you want a relational database, use SQLite (directly or via one of the Objective-C wrappers such as FMDB), or a relational database server. However, you may still want to learn Core Data for use as an object graph management framework. In combination with Apple's controller classes and key-value binding compatible view widgets, you can implement a complete MVC architecture with very little code.

相关文章