Some finishing on core data (iii)

Source: Internet
Author: User

Some finishing on core data (iii)

The relationship between the four types of core Data stacks and their relationships is as follows:

    • Nsmanagedobjectmodel
    • Nspersistentstore
    • Nspersistentstorecoordinator
    • Nsmanagedobjectcontext

Handwritten core Data Stack, the new file inherits NSObject, and in which three objects are initialized in sequence:,, NSManagedObjectContext NSPersistentStoreCoordinator NSManagedObjectModel The specific code is implemented as follows:

1 //CoreDataStack.h2 3 #import<Foundation/Foundation.h>4 #import<CoreData/CoreData.h>5 6 @interfaceCoredatastack:nsobject7 8@property (nonatomic, strong) Nsmanagedobjectcontext *context;9 Ten- (void) Savecontext; One  A @end -  - //COREDATASTACK.M the  - #import "CoreDataStack.h" -  - @interfaceCoredatastack () +  -@property (nonatomic, strong) Nspersistentstorecoordinator *PSC; +@property (nonatomic, strong) Nsmanagedobjectmodel *Managedobjectmodel; A  at @end -  - @implementationCoredatastack -  - @synthesizeManagedobjectmodel =_managedobjectmodel; -  in- (void) Savecontext { -   if(_context) { toNserror *error =Nil; +     if([_context haschanges] && [_context save:&ERROR]) { -NSLog (@"%@", error.localizeddescription); the abort (); *     } $   }Panax Notoginseng } -  the-(Nsurl *) Applicationdocumentsdirectory { +Nsarray *array =[[Nsfilemanager Defaultmanager] urlsfordirectory:nsdocumentdirectory indomains:nsuserdomainmask]; A   returnArray.lastobject; the } +  --(Nsmanagedobjectcontext *) Context { $   if(_context) { $     return_context; -   } -_context =[[Nsmanagedobjectcontext alloc] initwithconcurrencytype:nsmainqueueconcurrencytype]; the [_context setpersistentstorecoordinator:[self PSC]]; -   return_context;Wuyi } the  --(Nspersistentstorecoordinator *) PSC { Wu   if(_PSC) { -     return_PSC; About   } $_PSC =[[Nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:[self Managedobjectmodel]]; -Nsurl *url = [[Self applicationdocumentsdirectory] urlbyappendingpathcomponent:@"Jmdog_walk.sqlite"]; -[_PSC addpersistentstorewithtype:nssqlitestoretype Configuration:nil Url:url options:@{ nsmigratepersistentstoresautomaticallyoption:@1} Error:nil]; -   return_PSC; A } +  the-(Nsmanagedobjectmodel *) Managedobjectmodel { -   if(_managedobjectmodel) { $     return_managedobjectmodel; the   } theNsurl *url = [[NSBundle mainbundle] Urlforresource:@"Jmdog_walk"Withextension:@"MOMD"]; the_managedobjectmodel =[[Nsmanagedobjectmodel alloc] initwithcontentsofurl:url]; the   return_managedobjectmodel; - } in  the @end

The places to be noted are:

    1. All global variables are lazy loaded, improving efficiency;
    2. Just put it NSManagedObjectContext in the. h to go;
    3. Call Addpersistentstorewithtype: Note that the database file is named as the type of the SQLite suffix, otherwise it will be an error;
    4. Note that the naming consistency is used in the code JMDog_Walk , and when you build the core data file, you also name the file JMDog_Walk !

Some finishing on core data (iii)

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.