實(shí)現(xiàn)效果 :
CoreData的上下文編輯包括:添加, 刪除,查詢和修改幾個(gè)方面.
<b>實(shí)現(xiàn)效果: 在頁面上添加4個(gè)button, 分別觸發(fā)以上四個(gè)不同的事件.</b>
第一部分: 導(dǎo)入的相關(guān)文件,及設(shè)置上下文屬性
//導(dǎo)入CoreData框架
#import <CoreData/CoreData.h>
//導(dǎo)入AppDelegate,因?yàn)槲覀冃枰玫剿纳舷挛?#import "AppDelegate.h"
//導(dǎo)入實(shí)體類
#import "Student.h"
@interface ViewController ()
{
AppDelegate *appDelegate;
}
@property (strong, nonatomic)NSManagedObjectContext *mangaedObjectContext;
@end
@implementation ViewController
第二部分: 獲取上下文, 布局頁面
- (void)viewDidLoad {
[super viewDidLoad];
//獲取上下文
appDelegate = [UIApplication sharedApplication].delegate;
self.mangaedObjectContext = appDelegate.managedObjectContext;
//給按鈕添加標(biāo)題
NSArray *array = @[@"插入", @"刪除", @"修改", @"查詢"];
//利用for循環(huán)初始化四個(gè)按鈕, 并分別設(shè)置不同的tag值
for (int i = 0; i < array.count; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(30+i*60, 100, 40, 30);
[button setTitle:array[i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
button.tag = 1000 + i;
[self.view addSubview:button];
}
}
第三部分: 在button的事件中寫一個(gè)分支語句, 根據(jù)用戶的點(diǎn)擊觸發(fā)不同的方法
- (void)button:(UIButton *)button{
switch (button.tag) {
case 1000:
//執(zhí)行插入
[self insert];
break;
case 1001:
//執(zhí)行刪除
[self delete];
break;
case 1002:
//執(zhí)行修改
[self update];
break;
case 1003:
//執(zhí)行查詢
[self select];
break;
default:
break;
}
}
第四部分: 下面我們來分別實(shí)現(xiàn)CoreData的上下文操作
添加:
1.創(chuàng)建實(shí)體描述類(指定描述的類 , 指定在哪一個(gè)上下文中插入)
2.創(chuàng)建一個(gè)實(shí)體類對象(指定使用的實(shí)體描述對象)
3.對實(shí)體類對象進(jìn)行賦值操作
4.利用上下文,save保存實(shí)體
*實(shí)現(xiàn)添加方法
- (void)insert{
//第一步: 創(chuàng)建實(shí)體描述
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:appDelegate.managedObjectContext];
//第二步: 創(chuàng)建實(shí)體類對象(創(chuàng)建實(shí)體 + 指定實(shí)體描述 + 添加到上下文)
Student *student = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:appDelegate.managedObjectContext];
//賦值
student.name = @"蹦沙卡拉卡";
student.age = @"20";
student.gender = @"女";
//第三步: 保存上下文
[appDelegate.managedObjectContext save:nil];
//打印沙盒路徑, 訪問數(shù)據(jù)庫
NSLog(@"path = %@", NSHomeDirectory());
}
查詢:
1.創(chuàng)建實(shí)體描述類(可選)
2.創(chuàng)建查詢請求(如果實(shí)體沒有實(shí)體描述類則在創(chuàng)建請求的時(shí)候需要用實(shí)體描述來初始化)
3.利用上希望,execute執(zhí)行查詢操作
4.存儲(chǔ)到數(shù)組中
- (void)select{
//第一步: 創(chuàng)建實(shí)體描述類
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:appDelegate.managedObjectContext];
//第二步: 創(chuàng)建請求
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//第三步: 確定建立請求的類
[request setEntity:entity];
//第四步: 執(zhí)行請求,將返回結(jié)果放到數(shù)組中
NSArray *array = [appDelegate.managedObjectContext executeFetchRequest:request error:nil];
for (Student *stu in array) {
NSLog(@"name = %@, age = %@, gender = %@", stu.name, stu.age, stu.gender);
}
}
刪除:
1.創(chuàng)建實(shí)體描述類(指定描述的類 , 指定在哪一個(gè)上下文中插入)
2.創(chuàng)建請求(相當(dāng)于查詢操作)
3.給請求指定被請求的實(shí)體
4.設(shè)置條件(謂詞)
5.給請求指定請求時(shí)的條件
6.利用上下文,執(zhí)行請求, 并將結(jié)果保存到數(shù)組中用來操作
7.如果得到請求得到結(jié)果, 那么遍歷結(jié)果數(shù)組利用上下文刪除得到的對象
8.保存上下文
- (void)delete{
//第一步: 創(chuàng)建實(shí)體描述類
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:appDelegate.managedObjectContext];
//第二步: 創(chuàng)建請求
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//第三步: 確定建立請求的類
[request setEntity:entity];
//第四步: 設(shè)置刪除條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@"蹦沙卡拉卡"];
//第五步: 確定請求條件
[request setPredicate:predicate];
//第六步: 利用上下文執(zhí)行請求, 并將結(jié)果返回給數(shù)組
NSArray *array = [appDelegate.managedObjectContext executeFetchRequest:request error:nil];
if (array.count) {
for (Student *stu in array) {
//利用上下文對象刪除
[appDelegate.managedObjectContext deleteObject:stu];
}
//保存上下文
[appDelegate.managedObjectContext save:nil];
}
}```
更新:
1.創(chuàng)建實(shí)體類
2.創(chuàng)建請求
3.確定建立請求的實(shí)體類
4.設(shè)置條件
5.確定請求的條件
6.利用上下文執(zhí)行請求, 并將結(jié)果放到數(shù)組中
7.遍歷數(shù)組執(zhí)行修改操作
eight. 保存上下文
```code
- (void)update{
//1. 讀取實(shí)體類
NSEntityDescription *entity = [NSEntityDescription entityForName:@"People" inManagedObjectContext:appDelegate.managedObjectContext];
//2. 建立請求
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//3. 確定建立請求的類
[request setEntity:entity];
//4. 設(shè)置條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age = %@", @"16"];
//5. 設(shè)置請求的條件
[request setPredicate:predicate];
//6. 遍歷操作
NSArray *array = [appDelegate.managedObjectContext executeFetchRequest:request error:nil];
if (array.count) {
for (Student *stu in array) {
//執(zhí)行修改
stu.name = @"小白";
}
[appDelegate.managedObjectContext save:nil];
NSLog(@"修改成功");
}else{
NSLog(@"修改失敗");
}
}```