CoeeData是蘋果公司封裝的數(shù)據(jù)持久化框架 在iOS3.0中開放 它允許用戶按照實(shí)體-屬性-模型組織數(shù)據(jù) 并以二進(jìn)制/XML/sqlite數(shù)據(jù)文件的格式進(jìn)行持久化
CoreData的優(yōu)勢 他是蘋果公司的原生態(tài)的產(chǎn)品 他可以節(jié)省代碼量 大概是30%-70%,它支持可視化建模,CoreData支持?jǐn)?shù)據(jù)庫版本升級(jí)
屏幕快照 2016-03-07 下午7.46.27.png
首先點(diǎn)擊圖中紅線標(biāo)注的地方 之后點(diǎn)擊add Edtity添加類 改類的名字 在Attributes中添加類是屬性之后點(diǎn)擊之后步驟看圖
屏幕快照 2016-03-07 下午7.51.04.png
點(diǎn)next
屏幕快照 2016-03-07 下午7.51.20.png
屏幕快照 2016-03-07 下午7.51.38.png
記得options記得打鉤 之后創(chuàng)建
創(chuàng)建ViewController文件 ViewController.h聲明
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface CoreDataViewController : UITableViewController
//創(chuàng)建一個(gè)屬性 指定我們的對(duì)象
@property(nonatomic,strong)AppDelegate *myAppDelegate;
//創(chuàng)建一個(gè)上下文對(duì)象 用于處理所有與存儲(chǔ)相關(guān)的請(qǐng)求
@property(nonatomic,strong)NSManagedObjectContext *myContext;
//創(chuàng)建一個(gè)數(shù)組 用于存儲(chǔ)數(shù)組的數(shù)據(jù)源
@property(nonatomic,strong)NSMutableArray *allData;
@end
ViewController.m 這里用可視化
屏幕快照 2016-03-07 下午7.57.45.png
添加按鈕事件在ViewController.m 下面代碼實(shí)現(xiàn)CoreData的增 刪 改功能
- (void)viewDidLoad {
[super viewDidLoad];
//進(jìn)行數(shù)據(jù)初始化
AppDelegate *dele = [UIApplication sharedApplication].delegate;
self.myContext = dele.managedObjectContext;
self.allData = [NSMutableArray array];
//通過CoreData讀取本地所有的數(shù)據(jù)
[self getAllDataFromCoreData];
}
//通過CoreData讀取本地所有的數(shù)據(jù)
-(void)getAllDataFromCoreData{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.myContext];
[fetchRequest setEntity:entity];
// Specify criteria for filtering which objects to fetch
//謂詞
// Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age"
ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
NSError *error = nil;
NSArray *fetchedObjects = [self.myContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
NSLog(@"兩手空空你讓我如何盆滿缽滿");
}
//將查詢到的數(shù)據(jù)添加到數(shù)據(jù)源
[self.allData addObjectsFromArray:fetchedObjects];
//重新加載tableView
[self.tableView reloadData];
}
//添加數(shù)組
- (IBAction)addAction:(UIBarButtonItem *)sender {
//1 創(chuàng)建student對(duì)象
//創(chuàng)建于一個(gè)實(shí)體描述
NSEntityDescription *stuDis = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.myContext];
// Student *stu = [[Student alloc]init];
Student *stu = [[Student alloc]initWithEntity:stuDis insertIntoManagedObjectContext:self.myContext];
//給屬性賦值
stu.name = @"張三";
stu.age = arc4random() % 73 + 1;
//1 修改數(shù)據(jù)源
[self.allData addObject:stu];
//2 修改界面
NSIndexPath *path = [NSIndexPath indexPathForRow:self.allData.count - 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
//將數(shù)據(jù)保存到文件中進(jìn)行持久化
NSError *error = nil;
[self.myContext save:&error];
if (nil != error) {
NSLog(@"數(shù)據(jù)庫存儲(chǔ)持久化存在問題");
}
[((AppDelegate *)[UIApplication sharedApplication].delegate) saveContext];
}
//當(dāng)點(diǎn)擊tableViewCell的刪除按鈕的時(shí)候會(huì)調(diào)用 (當(dāng)提交編輯請(qǐng)求的時(shí)候回調(diào)用)
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//1 獲取當(dāng)前cell代表的數(shù)據(jù)
Student *stu = self.allData[indexPath.row];
//2 更新數(shù)據(jù)源
[self.allData removeObject:stu];
//3 更新UI
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//4 將臨時(shí)數(shù)據(jù)庫里進(jìn)行刪除 并進(jìn)行本地?cái)?shù)據(jù)持久化
[self.myContext deleteObject:stu];
[self.myContext save:nil];
}
}
//點(diǎn)擊cell的響應(yīng)事件(修改數(shù)據(jù))
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//1 更新第一步: 先查詢(FET)
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.myContext];
[fetchRequest setEntity:entity];
// Specify criteria for filtering which objects to fetch
// Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age"
ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
NSError *error = nil;
NSArray *fetchedObjects = [self.myContext executeFetchRequest:fetchRequest error:&error];
// 修改對(duì)應(yīng)的數(shù)據(jù)
Student *stu = self.allData[indexPath.row];
stu.name = @"尼古拉斯.趙四";
//更新數(shù)據(jù)源
[self.allData removeAllObjects];
[self.allData addObjectsFromArray:fetchedObjects];
//更新頁面
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//將修改本地持久化
[self.myContext save:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"main_cell" forIndexPath:indexPath];
Student *stu = self.allData[indexPath.row];
cell.textLabel.text = stu
.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",stu.age];
// Configure the cell...
return cell;
}
以上代碼實(shí)現(xiàn)了CoreData的簡單增 刪 改功能 希望對(duì)你有所幫助
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者