一:什么是CoreData?
CoreData
是蘋果公司封裝的進行數(shù)據(jù)持久化的框架,首次在IOS3.0版本的系統(tǒng)中出現(xiàn),它允許按照實體--屬性--值的模型組織數(shù)據(jù),并以XML
,二進制文件或者SQLite
數(shù)據(jù)文件的格式持久化數(shù)據(jù)
(1)CoreData
不是數(shù)據(jù)庫,它只是操作數(shù)據(jù)庫的框架
(2)CoreData
不僅僅可以對數(shù)據(jù)庫進行操作,而且還可以對xml
和二進制文件進行操作
(3)可以節(jié)省代碼量,一般要節(jié)省30%到70%的代碼量
二:效果展示
三:基本配置
(1)勾選"Use Core Data
"
四:布局配置
(1)去掉"Use Auto Layout
"和"Use Size Classes
"
(2)可視化添加頂部導(dǎo)航欄
在"Editor
"-->"Embed In
"-->選擇"Navigation Controller
"即可在模型上直接添加
(3)在組件中搜索"bar
"找到item
,拖拽到Navigation
上面,并且可以對其進行自定義.
樣式,樣色,位置全部都可以進行自定義
(4)添加TableView
,找到灰色的"TableView
",將其拖拽到ViewController
的正中間的位置,其大小可以進行拖拽來進行控制
(5)選擇"Table View
",點擊最右鍵,將"dataSource
"和"delegate
"對"View Controller
"進行關(guān)聯(lián)
(6)點擊"Table View
"中的"Prototype Cells
"屬性,其表示的其實是顯示的列數(shù)
表現(xiàn)為展示的一列
五:數(shù)據(jù)庫操作
(1)數(shù)據(jù)庫添加實體對象:點擊"SQLiteTest.xcdatamodeld
"
(2)添加實體對象"Clothes
",其實這個實體對象相當于數(shù)據(jù)庫中的表
在該對象(表)中,添加屬性
點擊"
Editor
"-->"Create NSManagedObject Subclass
"-->將會生成關(guān)于"Clohes
"的實現(xiàn)-->
這個地方需要注意,如果在原來的Entity進行重命名,生成的數(shù)據(jù)庫表名仍然為原來的值,唯一的辦法是刪掉重新寫
此外,在"
AppDelegate.h
"中還會自動生成三個對象和兩個方法
//
// AppDelegate.h
// SQLiteTest
//
// Created by AqiuBeats on 16/10/10.
// Copyright ? 2016年 AqiuBeats. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
/**
* 被管理對象上下文(數(shù)據(jù)管理器),相當于一個臨時數(shù)據(jù)庫(可視化建模的文件)
*/
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
/**
* 被管理對象模型(數(shù)據(jù)模型器)
*/
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
/**
* 持久化儲存助理(數(shù)據(jù)鏈接器),整個CoreData框架的核心
*/
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
/**
* 把我們臨時數(shù)據(jù)庫中進行的改變進行永久保存
*/
- (void)saveContext;
/**
* 獲取真實文件的儲存路徑
*/
- (NSURL *)applicationDocumentsDirectory;
@end
(3)對TableView
中的"Cell
"進行設(shè)置
在對行數(shù)設(shè)置為1之后
將該
cell
的樣式設(shè)置為basic
(4)主體代碼
//
// ViewController.m
// SQLiteTest
//
// Created by AqiuBeats on 16/10/10.
// Copyright ? 2016年 AqiuBeats. All rights reserved.
//
#import "ViewController.h"
#import "AppDelegate.h"
#import "Clothes+CoreDataProperties.h"
#import "Clothes.h"
/**
* 設(shè)置"UITableViewDelegate"和"UITableViewDataSource"可以獲取TableView的使用方法
*/
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
- (IBAction)addModel:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property(nonatomic,strong)NSMutableArray* dataSource;
//聲明一個AppDelegate對象屬性,來調(diào)用類中屬性,比如被管理對象個上下文
@property(nonatomic,strong)AppDelegate* myAppDelegate;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化數(shù)組
self.dataSource=[NSMutableArray array];
//初始化AppDelegate
self.myAppDelegate=[UIApplication sharedApplication].delegate;
//對TableView加上注冊方法,"cell"表示的是自定義的方法
[self.tableview registerClass :[UITableViewCell class] forCellReuseIdentifier:@"cell"];
#warning 查--查詢數(shù)據(jù)
//1.NSFetchRequest對象
NSFetchRequest* request=[[NSFetchRequest alloc]initWithEntityName:@"Clothes"];
//2.設(shè)置排序
// //2.1創(chuàng)建排序描述對象(以int類型的價格為例,進行升序排列)
// NSSortDescriptor *sortFunc=[[NSSortDescriptor alloc]initWithKey:@"price" ascending:YES];
// request.sortDescriptors=@[sortFunc];
//3.執(zhí)行這個查詢請求
NSError* error=nil;
NSArray *arr=[self.myAppDelegate.managedObjectContext executeFetchRequest:request error:&error];
//給數(shù)據(jù)源數(shù)組中添加數(shù)據(jù)
[self.dataSource addObjectsFromArray:arr];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//tableView的delegate和dataSource的方法
/**
* 返回分區(qū)中的行數(shù),相當于listview的item數(shù)目
*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataSource.count;
}
/**
* 返回分區(qū)的個數(shù)
*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
/**
* 對每個cell進行構(gòu)造,相當于listview的item
*/
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
Clothes* cloth=self.dataSource[indexPath.row];
cell.textLabel.text=[NSString stringWithFormat:@"品牌:%@--價格:%@",cloth.brand,cloth.price];
return cell;
}
//允許tableView可編輯,這樣就可以手動進行編輯了
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//對tableview的item可以進行各種手勢操作
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//滑動刪除樣式
if (editingStyle==UITableViewCellEditingStyleDelete) {
#warning 刪--刪除數(shù)據(jù),并對視圖進行實時更新
//刪除數(shù)據(jù)源
Clothes *cloth=self.dataSource[indexPath.row];
[self.dataSource removeObject:cloth];
//刪除數(shù)據(jù)管理中的數(shù)據(jù)
[self.myAppDelegate.managedObjectContext deleteObject:cloth];
//將刪除的更改進行永久保存
[self.myAppDelegate saveContext];
//刪除單元格
[self.tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
#warning 改--更改數(shù)據(jù)的屬性值,并對視圖進行實時更新
//點擊cell來修改數(shù)據(jù)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"當前位置%ld",(long)indexPath.row);
//1.先找到模型對象
Clothes* cloth=self.dataSource[indexPath.row];
//2.將該熟悉值更改
cloth.brand=@"Adidas";
//3.刷新視圖
[self.tableview reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//4.對數(shù)據(jù)的更改進行永久的保存
[self.myAppDelegate saveContext];
}
/**
* 插入數(shù)據(jù)
*/
- (IBAction)addModel:(id)sender {
#warning 增--插入數(shù)據(jù)
//創(chuàng)建實體描述
NSEntityDescription* description=[NSEntityDescription entityForName:@"Clothes" inManagedObjectContext:self.myAppDelegate.managedObjectContext];
//1.先創(chuàng)建一個模型對象
Clothes* cloth=[[Clothes alloc]initWithEntity:description insertIntoManagedObjectContext:self.myAppDelegate.managedObjectContext];
//2.對Clothe的對象屬性進行賦值
cloth.brand=@"Puma";
int priceCC=arc4random()%1000+1;
cloth.price=[NSNumber numberWithInt:priceCC];
//插入數(shù)據(jù)源數(shù)組(數(shù)組是可以存儲實體對象的)
[self.dataSource addObject:cloth];
//插入UI
[self.tableview insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataSource.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
//對數(shù)據(jù)管理器中的更改進行永久存儲
[self.myAppDelegate saveContext];
NSLog(@"%@",NSHomeDirectory());
}
@end
六:可視化SQLite文件
(1)所需要的工具:FireFox瀏覽器的插件
(2)獲取數(shù)據(jù)庫文件
NSLog(@"%@",NSHomeDirectory())
來獲取document文件夾地址
注意:要想獲得完整的數(shù)據(jù)庫表的數(shù)據(jù),以下三個文件一個都不能少!!!@@!
(3)使用火狐插件打開該SQLite文件,通過該文件的形式可以證明我的猜想,生成的實體類即為一張表
該表命名的規(guī)則"Z"+"Clothes"(大寫轉(zhuǎn)化)
可以可視化預(yù)覽表中內(nèi)容,其數(shù)據(jù)庫字段寫法為"Z"+"price"(大寫轉(zhuǎn)化)和"Z"+"brand"(大寫轉(zhuǎn)化)
源碼地址:https://github.com/AqiuBeats/SQLiteTest
完畢!