Core Data of iOS development learning

Source: Internet
Author: User

1. Add a Datamodel file

2. Adding entities and attributes

3. Create a subclass of Nsmanagedobject, which is named location (if no entity attribute type is transformable or no additional method, only need to store basic type, can skip) Note: Here you can do an extra work at location +coredataproperties.h file, change the type of the entity attribute type to be transformable corresponding to the @property property from the ID to the desired type 4. Add the following code to the APPDELEGATE.M file:

@interface Appdelegate ()

@property (Nonatomic,strong) Nsmanagedobjectcontext *managedobjectcontext;

@property (Nonatomic,strong) Nsmanagedobjectmodel *managedobjectmodel;

@property (Nonatomic,strong) Nspersistentstorecoordinator *persistentstorecoordinator;

@end

5. Add the following code at the bottom of the @implementation appdelegate.m file:

#pragma mark-core Data

-(Nsmanagedobjectmodel *) Managedobjectmodel

{

if (_managedobjectmodel = = nil)

{

NSString *modelpath = [[NSBundle mainbundle] pathforresource:@ "Datamodel" oftype:@ "MOMD"];

Nsurl *modelurl = [Nsurl Fileurlwithpath:modelpath];

_managedobjectmodel = [[Nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl];

}

return _managedobjectmodel;

}

-(NSString *) documentsdirectory

{

Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);

NSString *documentsdirectory = [Paths lastobject];

NSLog (@ "%@", documentsdirectory);

return documentsdirectory;

}

-(NSString *) Datastorepath

{

return [[Self documentsdirectory] stringbyappendingpathcomponent:@ "Datastore.sqlite"];

}

-(Nspersistentstorecoordinator *) persistentstorecoordinator

{

if (_persistentstorecoordinator = = nil)

{

Nsurl *storeurl = [Nsurl fileurlwithpath:[self Datastorepath];

_persistentstorecoordinator = [[Nspersistentstorecoordinator alloc] Initwithmanagedobjectmodel: Self.managedobjectmodel];

Nserror *error;

if (![ _persistentstorecoordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil URL:storeURL options: Nil Error:&error])

{

NSLog (@ "Error adding persistent store%@,%@", Error,[error UserInfo]);

Abort ();

}

}

return _persistentstorecoordinator;

}

-(Nsmanagedobjectcontext *) Managedobjectcontext

{

if (_managedobjectcontext = = nil)

{

Nspersistentstorecoordinator *coordinator = Self.persistentstorecoordinator;

if (Coordinator! = nil)

{

_managedobjectcontext = [[Nsmanagedobjectcontext alloc] init];

[_managedobjectcontext Setpersistentstorecoordinator:coordinator];

}

}

return _managedobjectcontext;

}

6. Add the following code to the. h header file of the view controller that requires the data manipulation of the core database:

@property (Nonatomic,strong) Nsmanagedobjectcontext *managedobjectcontext;

7. #import the. h file for the above view controller in the APPDELEGATE.M file, and pass the Managedobjectcontext pointer created by Appdelegate to the above controller

Such as:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {

Override point for customization after application launch.

Uitabbarcontroller *tabbarcontroller = (Uitabbarcontroller *) Self.window.rootViewController;

Currentlocationviewcontroller *currentlocationviewcontroller = (Currentlocationviewcontroller *) Tabbarcontroller.viewcontrollers[0];

Currentlocationviewcontroller.managedobjectcontext = self.managedobjectcontext;//Pass pointer

Uinavigationcontroller *navigationcontroller = (Uinavigationcontroller *) tabbarcontroller.viewcontrollers[1];

Locationsviewcontroller *locationsviewcontroller = (Locationsviewcontroller *) navigationcontroller.viewcontrollers [0];

Locationsviewcontroller.managedobjectcontext = self.managedobjectcontext;//Pass pointer

Mapviewcontroller *mapviewcontroller = (Mapviewcontroller *) tabbarcontroller.viewcontrollers[2];

Mapviewcontroller.managedobjectcontext = self.managedobjectcontext;//Pass pointer

return YES;

}

When the above view controller switches to another view controller that needs to have core data manipulated, pass the Managedobjectcontext pointer over the Prepareforsegue

Such as:

-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender

{

if ([Segue.identifier isequaltostring:@ "Taglocation"])

{

Uinavigationcontroller *navigationcontroller = Segue.destinationviewcontroller;

Locationdetailsviewcontroller *controller = (Locationdetailsviewcontroller *) Navigationcontroller.topviewcontroller;

Controller.coordinate = _location.coordinate;

Controller.placemark = _placemark;

Controller.managedobjectcontext = self.managedobjectcontext;//Pass pointer

}

}

(1) Add data :

(2) Modify the data :

(3) Delete data :

(4) find data :

  

Core Data of iOS development learning

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.