//Student 类是NSManagedObject类
// Person 是个表 是Core Data 里边的Data Model 。 如果已经创建过了,可以直接选NSManagedObject subclass
// //
//
#import "ViewController.h"
#import "Student.h"
#define _Student_ty @"Student"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *_dataArr;
NSManagedObjectContext *_moc;
NSInteger _current;
}
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *age;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_current=-1;
_dataArr=[[NSMutableArray alloc]init];
//找到coreData文件路径,注意后缀需要用momd
NSString *path=[[NSBundle mainBundle]pathForResource:@"Person" ofType:@"momd"];
//目标对象模型(被管理对象上下文)
NSManagedObjectModel *modle=[[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path]];
//需要绑定的数据库路径
NSString *sqlPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Person.sqlite"];
//关联起来------>>>>>>>持久化存储协调
NSPersistentStoreCoordinator *coordinator=[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:modle];
NSError *error=nil;
//将Person的协调话和数据库关联起来
NSPersistentStore *store=[coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlPath] options:nil error:&error];
if (!store) {
NSLog(@"store error == %@",error);
}
//目标对象上下文,(用来管理对象)
_moc=[[NSManagedObjectContext alloc]init];
//将协调起设置给上下文(上下文就是数据的管理者)
_moc.persistentStoreCoordinator=coordinator;
/************************************************/
//创建一个查询请求
NSFetchRequest *request=[[NSFetchRequest alloc]initWithEntityName:_Student_ty];
//执行查询请求,执行的结果是所有的实体对象
NSArray *resualtArr=[_moc executeFetchRequest:request error:&error];
if (error) {
NSLog(@"fetch error = %@",error);
}else{
_dataArr.array=resualtArr;
}
}
- (IBAction)btnClick:(UIButton *)sender {
[self.view endEditing:YES];
NSError *error=nil;
switch (sender.tag) {
case 1:
{
//插入一个实体,返回值就是一个实体的对象指针。
Student *stu=[NSEntityDescription insertNewObjectForEntityForName:_Student_ty inManagedObjectContext:_moc];
stu.name=_name.text;
//双重转化 先转化成int 然后再转化NSNumber
stu.age=[NSNumber numberWithInt:_age.text.intValue];
//保存并判断是否成功(增删改的操作都需要保存)
[_moc save:&error];
if ([_moc save:&error]) {
//插入数据源后刷新
[_dataArr addObject:stu];
[_myTableView reloadData];
}else{
NSLog(@"inset===error===%@",error);
}
}
break;
case 2:
{
if (_current<0) {
return;
}else{
//要删除的对象
Student *stu2=[_dataArr objectAtIndex:_current];
//删除
[_moc deleteObject:stu2];
//保存并判断是否成功
if ([_moc save:&error]) {
[_dataArr removeObjectAtIndex:_current];
[_myTableView reloadData];
}else{
NSLog(@"delete error = %@",error);
}
}
}
break;
case 3:
{
if (_current<0) {
return;
}else{
Student *stu3=_dataArr[_current];
stu3.name=_name.text;
//双重转化 先转NSInteger 再转NSNumber
stu3.age=[NSNumber numberWithInteger:_age.text.integerValue];
if ([_moc save:&error]) {
[_myTableView reloadData];
}else{
NSLog(@"upData === error====%@",error);
}
}
}
break;
case 4:
{
NSFetchRequest *request=[[NSFetchRequest alloc]initWithEntityName:_Student_ty];
//请求条件
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"name like %@",[NSString stringWithFormat:@"%@*",_name.text]];
//把请求条件附加给请求里边
request.predicate=predicate;
_dataArr.array=[_moc executeFetchRequest:request error:&error];
[_myTableView reloadData];
}
break;
default:
break;
}
_current=-1;
[email protected]"";
[email protected]"";
}
#pragma mark tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
}
Student *stu=_dataArr[indexPath.row];
cell.textLabel.text=stu.name;
cell.detailTextLabel.text=stu.age.stringValue;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_current=indexPath.row;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{//tableView发生滚动的时候收起键盘
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end