iphone:Core Data:Where does a Managed Object Context Come From?

简介:

Where a managed object context comes from is entirely application-dependent. In a Cocoa document-based application using NSPersistentDocument, the persistent document typically creates the context, and gives you access to it through the managedObjectContext method.

In a single-window application, if you create your project using the standard project assistant, the application delegate (the instance of the AppDelegate class) again creates the context, and gives you access to it through the managedObjectContext method. In this case, however, the code to create the context (and the rest of the Core Data stack) is explicit. It is written for you automatically as part of the template.

 

from core Data FQA: http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/coredata/Articles/cdFAQ.html#//apple_ref/doc/uid/TP40001802-CJBDBHCB

 

 

复制代码
    // 指定存储数据文件
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDirectory, YES);
    NSString *documentsDirectory = [paths lastObject];
    NSString *persistentStorePath = [documentsDirectory stringByAppendingPathComponent:@"Model.sqlite"];
    //创建托管对象模型
    NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    //创建持久化存储协调器,并使用SQLite数据库做持久化存储
    NSURL *storeUrl = [NSURL fileURLWithPath:persistentStorePath];
    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                                                                initWithManagedObjectModel:managedObjectModel];
    NSError *error = nil;
    NSPersistentStore *persistentStore = [persistentStoreCoordinator 
                                          addPersistentStoreWithType:NSSQLiteStoreType
                                          configuration:nil
                                          URL:storeUrl
                                          options:nil
                                          error:&error];

    //创建托管对象上下文
    NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc]init];
    [managedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator];
    
    self.context = managedObjectContext;
复制代码

http://www.cnblogs.com/mybkn/articles/2472881.html

   本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/04/27/2472861.html,如需转载请自行联系原作者
相关文章
|
存储 数据库 iOS开发
|
iOS开发
iPhone与iPad开发实战——精通Object C--视频
<p><a href="http://v.51work6.com/courseInfoRedirect.do?action=courseInfo&amp;courseId=240564">iPhone与iPad开发实战——精通Object C</a></p> <p><img src="http://img.blog.csdn.net/20130502161228914" height="150
1480 0
|
编解码 iOS开发
iphone 开发的基本入门知识
iphone 开发的基本入门知识
152 0
「镁客早报」iPhone或将在今年采用三摄;传Facebook致力于开发语音助力服务与亚马逊、苹果竞争
亚马逊向美国Alexa设备推免费音乐服务;视频会议软件开发商Zoom纳斯达克上市。
226 0
|
Web App开发 缓存 开发工具