Core Data uses

Source: Internet
Author: User

A brief introduction to the iOS data persistence Core uses


Words don't say much, direct


Next, command+n the new core Data file in the project


After the creation is successful, then edit the core Data file




Next, add attributes to the entity



Generate the corresponding model according to the added attributes


The User entity ticked the next step


The project will automatically generate the User's model

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface User:nsmanagedobject

@property (nonatomic, retain) NSString * username;
@property (nonatomic, retain) nsnumber * age;

@end


#import "User.h"

@implementation User

Dynamic dynamically generated settings or methods for accessing properties
@dynamic username;
@dynamic age;

@end


The next simple implementation of the database to add a message, to implement this feature requires a Mac or notebook installed


#import "ViewController.h"
#import <coredata/coredata.h>// Remember to import the core Data framework
#import "User.h"


#define Coredata_path [Nshomedirectory () stringbyappendingstring:@ "/library/Coredata.sqlite"]//sandbox path


@interface Viewcontroller ()
{

Nsmanagedobjectcontext *context;//File Management Objects
}
@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];


NSString *storepath=[nshomedirectory () stringbyappendingstring:@ "/library/Coredata.sqlite"];

NSLog (@ "%@", storepath);//Print the sandbox path of the database file
1. Connect and open the database
[Self opendatabase];


2. Add Data
[Self addUser];

}

-(void) OpenDatabase
{
//1. Create a data model object
Nsurl *mourl=[[nsbundle Mainbundle] urlforresource:@ "Model" withextension:@ "MOMD"];
Data Model objects
Nsmanagedobjectmodel * Managermodel=[[nsmanagedobjectmodel alloc] initwithcontentsofurl:mourl];


//2. Creating a local persisted file object

Nspersistentstorecoordinator *persist=[[nspersistentstorecoordinator Alloc] Initwithmanagedobjectmodel: Managermodel];
/*
Nssqlitestoretype is stored as a database type (and XML type storage)
URLs stored in sandboxed paths
*/
NSString *storepath=[nshomedirectory () stringbyappendingstring:@ "/library/coredata.sqlite"];
//

Nsurl *storeurl=[nsurl Fileurlwithpath:coredata_path];

Nserror *error=nil;

To add a data object to the databaseNssqlitestoretypeThis isSQLiteof the database type
[Persist Addpersistentstorewithtype:Nssqlitestoretype
Configuration:nil
Url:storeurl
Options:nil
error:&error];

if (Error) {

NSLog (@ "Database open failed");
}

//3. Creating a File Management Object
Context=[[nsmanagedobjectcontext alloc] init];

Managing local persisted file objects
[Context Setpersistentstorecoordinator:persist];

}


Add user
-(void) AddUser
{
Create Model Entities
(1) The name of the entity
(2) File Management Objects
user *user=[nsentitydescription insertnewobjectforentityforname:@ "user" inmanagedobjectcontext:context];
[Email protected] "Jack";
[Email protected];

Set to the File Management Objects container
(1) Nsmanagedobject object user-> is its entity
[Context Insertobject:user];
Save to Database
BOOL Result=[context Save:nil];

if (result) {

NSLog (@ "add success");
}else
{
NSLog (@ "Add failed");
}

}

The Core Data is briefly introduced here. The next update will continue.


Core Data uses

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.