IPhone: Core Data: Where does a managed object context come from?

Source: Internet
Author: User

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 throughManagedobjectcontextMethod.

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 throughManagedobjectcontextMethod. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.