In recent days has been using CoreData, previously felt that it is a sophisticated technology, and so they used to feel that it is so approachable.
One: First of all, we need to select the Usecoredata option when creating the application, and of course we can create it later.
The system will generate the following AppDelegate.h files
#import <UIKit/UIKit.h>
@interface appdelegate:uiresponder <UIApplicationDelegate>
@property ( Strong, nonatomic) UIWindow *window;
The context object
@property (readonly, Strong, nonatomic) Nsmanagedobjectcontext *managedobjectcontext;
Data Model
@property (readonly, Strong, nonatomic) Nsmanagedobjectmodel *managedobjectmodel;
Coordinator
@property (ReadOnly, Strong, nonatomic) Nspersistentstorecoordinator *persistentstorecoordinator;
The method
-(void) Savecontext is invoked when the application terminates;
Obtain the application's Documents path, the database exists in this directory
-(Nsurl *) applicationdocumentsdirectory;
@end
The APPDELEGATE.M file will have several more ways to do the following:
-(void) Applicationwillterminate: (uiapplication *) application {//Save context object in this call (at the end of the program) [Self Savecontext];}
This method is invoked #pragma makr-save context-(void) Savecontext {//The program terminates unexpectedly nserror *error = nil;
Nsmanagedobjectcontext *managedobjectcontext = Self.managedobjectcontext; if (Managedobjectcontext!= nil) {if ([Managedobjectcontext haschanges] &&![
Managedobjectcontext Save:&error]) {NSLog (@ "unresolved error%@,%@", error, [error userInfo]);
Abort (); }} #pragma mark-core Data stack-(Nsmanagedobjectcontext *) Managedobjectcontext {//Create context object if (_
Managedobjectcontext!= Nil) {return _managedobjectcontext;
} nspersistentstorecoordinator *coordinator = [self persistentstorecoordinator];
if (Coordinator!= nil) {_managedobjectcontext = [[Nsmanagedobjectcontext alloc] init]; [_managedobjectcontext Setpersistentstorecoordinator:coordinAtor];
return _managedobjectcontext; }-(Nsmanagedobjectmodel *) Managedobjectmodel {//Create Data model if (_managedobjectmodel!= nil) {return _
Managedobjectmodel;
} nsurl *modelurl = [[NSBundle mainbundle] urlforresource:@ "CoreDataDemo02" withextension:@ "MOMD"];
_managedobjectmodel = [[Nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl];
return _managedobjectmodel; }-(Nspersistentstorecoordinator *) Persistentstorecoordinator {//Create Coordinator, that is, store if _persistentstorecoordinator!
= nil) {return _persistentstorecoordinator; } nsurl *storeurl = [[self applicationdocumentsdirectory] urlbyappendingpathcomponent:@ ' Coredatademo02.sqlite
"];
Nserror *error = nil; _persistentstorecoordinator = [[Nspersistentstorecoordinator alloc] Initwithmanagedobjectmodel:[self
Managedobjectmodel]]; if (![ _persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil URL:storeURL oPtions:nil Error:&error]) {NSLog (@ "unresolved error%@,%@", error, [error userInfo]);
Abort ();
return _persistentstorecoordinator;
#pragma mark-application ' s documents Directory-(Nsurl *) applicationdocumentsdirectory {//Get the Documents catalogue return [[[Nsfilemanager Defaultmanager] urlsfordirectory:nsdocumentdirectory Indomains:nsuserdomainmask] LastObject
]; }
This is the framework that the system has built for us, we just need to do data manipulation.