我已经非常熟悉关系数据库,并且在过去使用过SQLite(和其他数据库)。然而,Core Data有一定的吸引力,所以我正在考虑花一些时间学习它,以便在下一个应用程序中使用。

在SQLite上使用Core Data是否有很大的好处,反之亦然?它们各自的优点和缺点是什么?

我发现很难证明学习Core Data的成本是合理的,因为苹果的许多旗舰应用程序(如Mail)都没有使用Core Data。app或iPhoto。app -而不是选择SQLite数据库。SQLite在iPhone上也被广泛使用。

熟悉使用这两种方法的人能谈谈他们的经验吗?也许,就像大多数事情一样,这个问题比仅仅使用一种而不是另一种更深刻?


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

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

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

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.

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模式。相反,你不应该依赖于能够读取Core Data的SQLite数据存储与其他SQLite工具;模式是一个可能改变的实现细节。

因此,直接使用Core Data或SQLite之间实际上没有任何冲突。如果你想要一个关系数据库,使用SQLite(直接或通过一个Objective-C包装器,如FMDB),或一个关系数据库服务器。但是,您可能仍然希望学习Core Data作为对象图管理框架使用。结合苹果的控制器类和键值绑定兼容的视图小部件,你可以用很少的代码实现一个完整的MVC架构。

在iOS 5.0中,如果你使用的是Core Data,你还可以免费使用iCloud文件同步。如果你直接使用SQLite,那就需要大量的手工修改和实现才能在iCloud上实现同步。