CoreData 數(shù)據(jù)持久化框架是Cocoa API的一部分,它允許按照 實(shí)體-屬性-值 模型組織數(shù)據(jù),并以XML(做MAC-os用), 二進(jìn)制, 或者SQLite數(shù)據(jù)文件的格式持久化數(shù)據(jù). CoreData主要提供 對(duì)象-關(guān)系映射(ORM)功能,把OC對(duì)象轉(zhuǎn)化為數(shù)據(jù)保存到文件,也可以數(shù)據(jù)轉(zhuǎn)化成OC對(duì)象.
CoreData與SQLite區(qū)別 :
CoreData是官方推出的數(shù)據(jù)持久化框架,SQLite是蘋(píng)果使用別人開(kāi)發(fā)好的一個(gè)動(dòng)態(tài)庫(kù),本質(zhì)是關(guān)系型數(shù)據(jù)庫(kù).
CoreData是iOS平臺(tái)下的一個(gè)數(shù)據(jù)持久化的方式,不可以跨平臺(tái)使用,Sqlite可以跨平臺(tái)使用.
核心對(duì)象:
NSPersistentStoreCoordinator ?數(shù)據(jù)連接器類(lèi)(中間人,不能直接控制)
NSManagedObjectContext ? ? ? 數(shù)據(jù)管理器類(lèi)? (臨時(shí)數(shù)據(jù)庫(kù) )
NSManagedObject ? ? ? ? ? ? ? ? ? 數(shù)據(jù)管理類(lèi)
NSManagedObject ? ? ? ? ? ? ? ? ? 數(shù)據(jù)模型器類(lèi)
NSEntityDescription ? ? ? ? ? ? ? ? ?實(shí)體描述類(lèi)
操作過(guò)程: context想要獲取值,先要告訴連接器,我要什么東西,連接器再告訴store,你給我什么東西,store去找,找到之后返回給context.

數(shù)據(jù)庫(kù)的簡(jiǎn)單操作
.xcdatamodeld里

Add Entity 添加實(shí)體類(lèi)
修改實(shí)體類(lèi)名
修改屬性名與類(lèi)型
創(chuàng)建文件 ?command+N —> CoreData —> NSManageObjuect subclass
ViewController.m 里
導(dǎo)入框架及各種頭文件
//coreData的框架
//導(dǎo)入框架的方式: <框架名字/頭文件名字>
#import
#import"Person.h"
#import"AppDelegate.h"
@interfaceViewController()
@property(nonatomic,strong)UITableView*tableView;
//臨時(shí)數(shù)據(jù)庫(kù)
@property(nonatomic,strong)NSManagedObjectContext*objectContext;
//創(chuàng)建一個(gè)可變數(shù)組,配合table設(shè)置cell的相關(guān)設(shè)置
@property(nonatomic,strong)NSMutableArray*dataArray;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.dataArray= [NSMutableArrayarray];
[selfsetup];
//
[selfappdelegate];
//調(diào)用searchAll
[selfsearchAll];
}
- (void)setup{
self.tableView= [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].boundsstyle:(UITableViewStylePlain)];
self.tableView.backgroundColor= [UIColorredColor];
[self.viewaddSubview:self.tableView];
self.tableView.dataSource=self;
self.tableView.delegate=self;
[self.tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"cell"];
}
- (void)appdelegate{
//通過(guò)單例的代理協(xié)議的代理人,獲取到我們最開(kāi)始使用的AppDelegate
AppDelegate*app = (AppDelegate*)[UIApplicationsharedApplication].delegate;
//獲得 數(shù)據(jù)庫(kù)的數(shù)據(jù)連接器
self.objectContext= app.managedObjectContext;
self.navigationItem.rightBarButtonItem= [[UIBarButtonItemalloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd)target:selfaction:@selector(barButtonItemClicked)];
}
//點(diǎn)擊方法
- (void)barButtonItemClicked{
//NSEntityDescription :實(shí)體描述類(lèi),通過(guò)類(lèi)方法創(chuàng)建
//參數(shù)1 :表示這個(gè)實(shí)體描述類(lèi)描述的是哪個(gè)實(shí)體
//參數(shù)2 :表示的是在context里創(chuàng)建一個(gè)描述,告訴context我要往里面插入以object了
NSEntityDescription*description = [NSEntityDescriptionentityForName:@"Person"inManagedObjectContext:self.objectContext];
//創(chuàng)建一個(gè)實(shí)體類(lèi)
//參數(shù)1 :實(shí)體類(lèi)描述
//參數(shù)2 :在context里放入這個(gè)類(lèi)
Person*person = [[Personalloc]initWithEntity:descriptioninsertIntoManagedObjectContext:self.objectContext];
intnumber =arc4random() % 2000;
person.name= [NSStringstringWithFormat:@"%d號(hào)",number];
[selfinsertObject:person];
}
// 增向coreData中插入一條數(shù)據(jù)
- (void)insertObject:(Person*)person{
NSError*error =nil;
//把context保存到本地
//這個(gè)方法執(zhí)行之后,本地?cái)?shù)據(jù)才發(fā)生改變
[self.objectContextsave:&error];
if(error ==nil) {
NSLog(@"保存正確");
[self.dataArrayaddObject:person];
NSIndexPath*indexPath = [NSIndexPathindexPathForRow:self.dataArray.count-1inSection:0];
[self.tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:(UITableViewRowAnimationMiddle)];
}else{
NSLog(@"錯(cuò)誤%@",error);
}
}
// ?刪
-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
if(editingStyle ==UITableViewCellEditingStyleDelete) {
//獲取想要?jiǎng)h的數(shù)據(jù)
Person*person =self.dataArray[indexPath.row];
//在context中將這條數(shù)據(jù)刪除
[self.objectContextdeleteObject:person];
NSError*error =nil;
[self.objectContextsave:&error];
if(error ==nil) {
//先刪除數(shù)據(jù)
[self.dataArrayremoveObject:person];
//然后更新UI
[self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:(UITableViewRowAnimationFade)];
}
}
}
// 改
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
//知道改誰(shuí)
Person*person =self.dataArray[indexPath.row];
intnumber =arc4random() % 2000;
person.name= [NSStringstringWithFormat:@"%d好", number];
NSError*error =nil;
[self.objectContextsave:&error];
if(error ==nil) {
[self.tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:(UITableViewRowAnimationMiddle)];
}
}
// 查
- (void)searchAll{
//創(chuàng)建一個(gè)查詢操作,查詢哪個(gè)表里的內(nèi)容
NSFetchRequest*request = [[NSFetchRequestalloc]initWithEntityName:@"Person"];
//接收查詢數(shù)據(jù)
NSError*error =nil;
//
NSArray*array = [self.objectContextexecuteFetchRequest:requesterror:&error];
//判斷error
if(error ==nil) {
//如果是,那就放到dataArray里面
[self.dataArraysetArray:array];
[self.tableViewreloadData];
}else{
}
}
//tableView必須實(shí)現(xiàn)的方法
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
returnself.dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
//創(chuàng)建實(shí)例接收
Person*person =self.dataArray[indexPath.row];
cell.textLabel.text= person.name;
returncell;
}
@end
