Analysis of IOS master-Detail template application demo

Source: Internet
Author: User
Tags file url

This article requires a certain level of core data.

For example, create a project and select a template of the master-detail application type.

Name it mastercoredata. Check use core data and use automatic reference counting.

Test the project on the simulator. The results are as follows:

Click Add in the upper-right corner. The result is as follows:

Click the Add button in the upper right corner. The result is as follows:

Click the edit button in the upper left corner to enter the delete and edit mode, for example:

Try other operations without crashing. Ah, xcode has already implemented so much for us!

Next, let's take a look at what code xcode has automatically added for us and what is the call relationship between these codes. Let's go!

The project directory contains an additional file named mastercoredata. xcdatamodeld,

Click editor style in the lower-right corner to convert it to another display mode, for example:

Appdelegate. H has the following code more than normal:

@ Property (readonly,
Strong, nonatomic) nsmanagedobjectcontext * managedobjectcontext;

@ Property (readonly,
Strong, nonatomic) nsmanagedobjectmodel * managedobjectmodel;

@ Property (readonly,
Strong, nonatomic)
Nspersistentstorecoordinator * persistentstorecoordinator;

-(Void) savecontext;

-(Nsurl *) applicationdocumentsdirectory;

Appdelegate. M has the following code more than normal:

-(Void) savecontext

{

Nserror * error = nil;

Nsmanagedobjectcontext * managedobjectcontext =
Self. managedobjectcontext;

If (managedobjectcontext! =
Nil ){

If ([managedobjectcontext
Haschanges] &! [Managedobjectcontext save: & error]) {

// Replace this implementation with code to handle the error appropriately.

// Abort () causes the application to generate a crash log and terminate. You shocould not use this function in a shipping application, although it may be useful during development.

Nslog (@ "unresolved error % @, % @", error, [Error
Userinfo]);

Abort ();

}

}

}

# Pragma mark-core data stack

// Returns the managed object context for the application.

// If the context doesn' t already exist, it is created and bound to the persistent store coordinator for the application.

-(Nsmanagedobjectcontext *) managedobjectcontext

{

If (_ managedobjectcontext! =
Nil ){

Return
_ Managedobjectcontext;

}

Nspersistentstorecoordinator * Coordinator = [self
Persistentstorecoordinator];

If (Coordinator! = Nil ){

_ Managedobjectcontext = [[nsmanagedobjectcontext
Alloc] init];

[_ Managedobjectcontext
Setpersistentstorecoordinator: Coordinator];

}

Return
_ Managedobjectcontext;

}

// Returns the managed Object Model for the application.

// If the model doesn't already exist, it is created from the application's model.

-(Nsmanagedobjectmodel *) managedobjectmodel

{

If (_ managedobjectmodel! =
Nil ){

Return
_ Managedobjectmodel;

}

Nsurl * modelurl = [[nsbundle
Mainbundle] urlforresource: @ "mastercoredata"
Withextension: @ "momd"];

_ Managedobjectmodel = [nsmanagedobjectmodel
Alloc] initwithcontentsofurl: modelurl];

Return
_ Managedobjectmodel;

}

// Returns the persistent store coordinator for the application.

// If the coordinator doesn't already exist, it is created and the application's store added to it.

-(Nspersistentstorecoordinator *) persistentstorecoordinator

{

If (_ persistentstorecoordinator! =
Nil ){

Return
_ Persistentstorecoordinator;

}

Nsurl * storeurl = [[self
Applicationdocumentsdirectory] urlbyappendingpathcomponent: @ "mastercoredata. SQLite"];

Nserror * error = nil;

_ Persistentstorecoordinator = [[nspersistentstorecoordinator
Alloc] initwithmanagedobjectmodel: [self
Managedobjectmodel];

If (! [_ Persistentstorecoordinator
Addpersistentstorewithtype: nssqlitestoretype
Configuration: Nil
URL: storeurl options: Nil
Error: & error]) {

/*

Replace this implementation with code to handle the error appropriately.

Abort () causes the application to generate a crash log and terminate. You shocould not use this function in a shipping application, although it may be useful during development.

Typical reasons for an error here include:

* The persistent store is not accessible;

* The schema for the persistent store is incompatible with current managed object model.

Check the error message to determine what the actual problem was.

If the persistent store is not accessible, there is typically something wrong with the file path. Often, a File URL is pointing into the application's Resources Directory instead of a writeable directory.

If you encounter schema incompatibility errors during development, you can reduce their frequency:

* Simply deleting the existing store:

[[Nsfilemanager defaultmanager] removeitematurl: storeurl error: Nil]

* Specify Ming automatic lightweight migration by passing the following dictionary as the options parameter:

@ {Nsmigratepersistentstoresautomaticallyoption: @ Yes, nsinfermappingmodelautomaticallyoption: @ Yes}

Lightweight migration will only work for a limited set of Schema changes; Consult "core data model versioning and data migration programming guide" for details.

*/

Nslog (@ "unresolved error % @, % @", error, [Error
Userinfo]);

Abort ();

}

Return
_ Persistentstorecoordinator;

}

# Pragma mark-application's documents directory

// Returns the URL to the application's statements directory.

-(Nsurl *) applicationdocumentsdirectory

{

Return [[nsfilemanager
Defaultmanager] urlsfordirectory: nsdocumentdirectory
Indomains: nsuserdomainmask]
Lastobject];

}

Related Article

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.