An overview of CoreData's deletion, modification and check

Source: Internet
Author: User
Tags terminates uikit

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.

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.