Version COREDATAMANAGER-OC-compatible with previous versions of IOS10

Source: Internet
Author: User
Tags file url sqlite database

Tag: transaction instantiation void STR code NTS file res add

Header file:

1 #import<Foundation/Foundation.h>2 #import<CoreData/CoreData.h>3 4 /**5 CoreData Manager6  */7 @interfaceCoredatamanager:nsobject8 9+(instancetype) Sharedmanager;Ten  One /** A Persistence Container-can provide management context for IOS 10 launch - contains all core objects in the core Data stack, which are not thread-safe -   the -Nsmanagedobjectcontext *viewcontext; management context - -Nsmanagedobjectmodel *managedobjectmodel; - -Nspersistentstorecoordinator *persistentstorecoordinator; -  */ + //@property (readonly, strong) Nspersistentcontainer *persistentcontainer; -  + /** A Managing Object Contexts at  */ -@property (ReadOnly, strong) Nsmanagedobjectcontext *MoC; -  - /** - Save Context -  */ in- (void) Savecontext; -  to @end

. m File:

1 #import "CoreDataManager.h"2 3 @implementationCoredatamanager4 5+(instancetype) Sharedmanager {6     Static IDinstance;7     Staticdispatch_once_t Oncetoken;8Dispatch_once (&oncetoken, ^{9instance = [SelfNew];Ten     }); One     returninstance; A } -  - #pragmaMark-core Data Stack the //If you override the Getter method for a read-only property, the compiler no longer provides the _ member variable - @synthesizeMOC =_moc; -  - /** + for low-version compatibility -  */ +-(Nsmanagedobjectcontext *) MoC { A      at     if(_moc! =Nil) { -         return_moc; -     } -      -     //Mutex, you should lock the code as little as possible!  - @synchronized (self) { in          -         //1. Instantiating the management context to_moc =[[Nsmanagedobjectcontext alloc] initwithconcurrencytype:nsmainqueueconcurrencytype]; +          -         //2. Manage object Models (entities) theNsmanagedobjectmodel *mom =[Nsmanagedobjectmodel Mergedmodelfrombundles:nil]; *          $         //3. Persistent Storage SchedulerPanax NotoginsengNspersistentstorecoordinator *PSC =[[Nspersistentstorecoordinator alloc] initwithmanagedobjectmodel:mom]; -          the         //4. Adding a database +         /** A 1> data storage type the 3> Save the URL of the SQLite database file + 4> Setting Database Options -          */ $NSString *cachedir =nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES). Lastobject; $NSString *path = [Cachedir stringbyappendingpathcomponent:@"ys.db"]; -         //convert the full path of a local file to a file URL -Nsurl *url =[Nsurl Fileurlwithpath:path]; the          -Nsdictionary *options =@{nsmigratepersistentstoresautomaticallyoption: @ (YES),Wuyi nsinfermappingmodelautomaticallyoption: @ (YES)}; the          - [PSC addpersistentstorewithtype:nssqlitestoretype configuration:nil Url:url options:options ERROR:NULL]; Wu          -         //5. Specifying the storage scheduler for the management context About_moc.persistentstorecoordinator =PSC; $     } -      -     return_moc; - } A  + #pragmaMark-core Data Saving Support the- (void) Savecontext { -Nsmanagedobjectcontext *context =Self.moc; $      the     //determine if there are data changes in the context the     //' transaction ' can save multiple data, not necessarily every time the data changes need to be saved, for example: for adding more than one record, you can call the last save operation!  the     if(![Context HasChanges]) { the         return; -     } in      the     //Save Data theNserror *error =Nil; About      the     if(! [Context save:&ERROR]) { theNSLog (@"Error saving Data%@,%@", error, error.userinfo); the     } + } -  the @end

Version COREDATAMANAGER-OC-compatible with previous versions of IOS10

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.