macos 核心数据与 SQLite 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/523482/
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-10-21 06:02:36  来源:igfitidea点击:

Core Data vs SQLite 3

cocoamacoscore-datasqlite

提问by Jason Medeiros

I am already quite familiar with relational databases and have used SQLite(and other databases) in the past. However, Core Datahas 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?

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

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.

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

那些熟悉使用两者的人可以评论他们的经验吗?也许,与大多数事情一样,这个问题比仅仅使用一个比另一个更深刻?

采纳答案by Barry Wark

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

To implement its graph management, Core Data happensto use SQLite as a disk store. It couldhave 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 OmniDataObjectsframework, an open source implementation of a subset of the Core Data API. The BaseTenframework is also an implementation of the Core Data API using PostgreSQL as a backend.

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

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 的 ORM,所以它不能读取任意 SQLite 模式。相反,您不应该依赖于能够使用其他 SQLite 工具读取 Core Data 的 SQLite 数据存储;架构是一个可能会改变的实现细节。

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 verylittle code.

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

回答by strange

And with iOS 5.0 you get the added benefit of being able to use iCloud file-sync for free if you're using Core Data. If you're using SQLite directly it'll have to be a lot of manual tinkering and implementation to get it to sync across iCloud.

在 iOS 5.0 中,如果您使用 Core Data,您可以获得免费使用 iCloud 文件同步的额外好处。如果您直接使用 SQLite,则必须进行大量手动修改和实施才能使其在 iCloud 中同步。

回答by Joel Levin

Core Data isn't so much a database engine as it is an API that abstracts over the actual data store. You can tell Core Data to save as an sqlite database, a plist, a binary file, or even a custom data store type.

Core Data 与其说是一个数据库引擎,不如说是一个抽象了实际数据存储的 API。你可以告诉 Core Data 保存为 sqlite 数据库、plist、二进制文件,甚至自定义数据存储类型。

I would recommend learning Core Data, as is it an excellent resource that greatly accelerates many parts of cocoa application development.

我建议学习 Core Data,因为它是一种极好的资源,可以极大地加速可可应用程序开发的许多部分。

回答by cefstat

SQLite is one of the database formats for Core Data. Using Core Data you get better integration with the rest of the Cocoa API.

SQLite 是 Core Data 的数据库格式之一。使用 Core Data,您可以更好地与 Cocoa API 的其余部分集成。