Core Data是蘋果公司所開發的macOS作業系統(舊稱:Mac OS X及OS X)中Cocoa API的一部分,首次在Mac OS X 10.4 Tiger与iOS 3.0系统中出现。它允许按照实体-属性-值模型组织数据,并以XML,二进制文件或SQLite数据文件的格式将其序列化。Core Data允许用户使用代表实体和实体间关系的高层对象来操作数据。它也可以管理序列化的数据,提供对象生存期管理与object graph管理,包括存储。Core Data直接与SQLite交互,避免开发者使用原本的SQL语句。
就像Cocoa绑定在模型-视图-控制器设计中做了很多控制器的工作一样,Core Data做了很多数据模型的工作。它的主要任务是负责数据更改的管理,序列化到磁盘,最小化内存占用,以及查询数据。
專長於Core Data及Cocoa軟體開發的資訊圖書作家Marcus Zarra曾說:
使用方法
Core Data使用包括实体和实体间关系,以及查找符合某些条件的实体的请求等内容的高层数据模型描述数据。开发者可以在纯对象层上查找与管理这些数据,而不必担心存储和查找的实现细节。Interface Builder中的控制器对象可以直接获取与管理实体,若与Cocoa绑定结合,则可以在不写自定义代码的情况下让用户界面显示、修改与管理数据模型中的大部分内容。
例如:开发者希望开发一个管理vCard的程序。作者希望使用Core Data将一些vCard读入对象中,然后把它们存入一个大XML文件中。开发者需要在Xcode中建立数据模型,然后到Interface Builder中加入Core Data Entity项以创建其数据模型对应的GUI。然后,开发者可以加入标准的Objective-C代码以读取vCard,并将数据写入由Core Data管理的实体中。在此,开发者的代码是管理Core Data对象,而非其对应的vCard。最后,将Save菜单项连接到控制器对象对应的方法上,使得控制器在接到存储消息后自动的检查数据堆栈,确认数据是否合法,最后利用当前更改重写对应的Core Data文档。在整个过程中,开发者只需要专注于将vCard文件转换为Core Data实体的过程,而不需关心例如数据的显示、合法性检查、存储等其它部分,提高了开发效率。
Core Data is organized into a large hierarchy of classes, though interaction is only prevalent with a small set of them.
Core Data由相对庞大的类继承体系组成,但开发者需要关注的接口只是其中的一个相对小的子集。
存储格式
Core Data可以将数据存储为XML,二进制文件或SQLite文件。
Core Data是标准化的,可以自由的读写Xcode数据模型文件(通常是.xcdatamodel文件)。与EOF不同,Core Data目前没有设计多用户或多线程访问模式。模型迁移通常也需要代码,若其它开发者依赖于某个数据模型,则该数据模型的设计者可能在模型发生改变时需要与新数据模型一起提供版本转换代码。
历史与发展
Core Data的很大一部分设计都归功于早期的NeXT产品,(EOF)。
脚注
参考文献
- 苹果公司 (2009年9月17日). "Core Data Programming Guide". Retrieved from [http://developer.apple.com/mac/library/documentation/cocoa/conceptual/CoreData/cdProgrammingGuide.html http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html]
- 苹果公司 (2009年9月). "Core Data Tutorial for iPhone OS". Retrieved from [https://web.archive.org/web/20100323200755/http://developer.apple.com/iPhone/library/documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html https://web.archive.org/web/20100323200755/http://developer.apple.com/iPhone/library/documentation/DataManagement/Conceptual/iPhoneCoreData01/Introduction/Introduction.html]
- 苹果公司 (2010年6月14日). Predicate Programming Guide. 来自[http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Predicates/predicates.html%23//apple_ref/doc/uid/TP40001789]
- 苹果公司 (2006年). "EOModeler User Guide". Retrieved from [http://developer.apple.com/legacy/mac/library/documentation/WebObjects/UsingEOModeler/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001018-CH201-TP1 http://developer.apple.com/legacy/mac/library/documentation/WebObjects/UsingEOModeler/Introduction/Introduction.html#//apple_ref/doc/uid/TP30001018-CH201-TP1]
- Jurewitz, M. & Apple Inc. (2010). "iPhone Development Videos: Working With Core Data". Retrieved from [http://developer.apple.com/videos/iphone/#video-advanced-coredata http://developer.apple.com/videos/iphone/#video-advanced-coredata]
- Stevenson, S. (2005). "Core Data Class Overview". Retrieved from [http://cocoadevcentral.com/articles/000086.php http://cocoadevcentral.com/articles/000086.php]
- Zarra, M. S. (2009). Core Data Apple's API for Persisting Data on Mac OS X. The Pragmatic Programmers.
- LaMarche, J., & Mark, D. (2009). More iPhone 3 Development: Tackling iPhone SDK 3. Apress.
外部链接
- Apple Inc. (2006). "Developing With Core Data". Retrieved from [http://developer.apple.com/macosx/coredata.html http://developer.apple.com/macosx/coredata.html]
- Apple Inc. (2009). "Web Objects Tutorial". Retrieved from [http://developer.apple.com/legacy/mac/library/documentation/DeveloperTools/Conceptual/WOTutorial/Introduction/Introduction.html http://developer.apple.com/legacy/mac/library/documentation/DeveloperTools/Conceptual/WOTutorial/Introduction/Introduction.html]
- CocoaDev. (n.d.). Retrieved from [https://web.archive.org/web/20080801025517/http://www.cocoadev.com/ https://web.archive.org/web/20080801025517/http://www.cocoadev.com/]
- Stevenson, S. (2005). "Build A Core Data Application". Retrieved from [http://cocoadevcentral.com/articles/000085.php http://cocoadevcentral.com/articles/000085.php]
评论 (0)