Where a managed object context comes from is entirely application-dependent. In a cocoa document-based application usingNspersistentdocument
, The persistent document typically creates the context, and gives you access to it throughManagedobjectcontext
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 throughManagedobjectcontext
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
// Data File Storage Nsarray * paths = Nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdirectory, yes); nsstring * Documentsdirectory = [Paths lastobject]; nsstring * Persistentstorepath = [documentsdirectory stringbyappendingpathcomponent: @" Model. SQLite " ]; // Create a managed Object Model Nsmanagedobjectmodel * managedobjectmodel =[Nsmanagedobjectmodel mergedmodelfrombundles: Nil]; // Create a persistent Storage Coordinator and use the SQLite database for persistent Storage 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]; // Create managed object context Nsmanagedobjectcontext * managedobjectcontext = [[Nsmanagedobjectcontext alloc] init]; [managedobjectcontext setpersistentstorecoordinator: persistentstorecoordinator]; self. Context = Managedobjectcontext;
Http://www.cnblogs.com/mybkn/articles/2472881.html