使用CoreData存储数据

时间:2021-01-08 14:45:59

- (void)viewDidLoad {

[super viewDidLoad];

//获取模型文件的路径

NSString *path=[[NSBundle mainBundle]pathForResource:@"Cart" ofType:@"momd"];

//利用模型文件创建被管理模型对象

NSManagedObjectModel *managedObjectModel=[[NSManagedObjectModel alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path]];

//创建持久化存储协调器对象

NSPersistentStoreCoordinator *persistentStoreCoordinator=[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:managedObjectModel];

//指定持久化存储文件的路径

NSString *dbPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/cart1.sqlite"];

//添加一个持久化存储对象,第1个参数指存储的类型,url:指持久化存储的文件路径

[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dbPath] options:nil error:nil];

//初始化对象

_managedObjectContext=[[NSManagedObjectContext alloc]init];

_managedObjectContext.persistentStoreCoordinator=persistentStoreCoordinator;

//[self addData];

//[self updateData];

[self deleteDataByID:2];

[self displayAllData];

}

//向数据文件中添加数据

-(void)addData

{

//根据模型图中的模型名获取新的实例对象

Customer *customer=[NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:_managedObjectContext];

customer.customerID=[NSNumber numberWithInt:2];

customer.userName=@"zhangsan";

customer.password=@"11111";

ShoppingCartMaster *master=[NSEntityDescription insertNewObjectForEntityForName:@"ShoppingCartMaster" inManagedObjectContext:_managedObjectContext];

master.cartID=[NSNumber numberWithInt:2000];

master.customerID=[NSNumber numberWithInt:2];

ShoppingCartDetail *detail1=[NSEntityDescription insertNewObjectForEntityForName:@"ShoppingCartDetail" inManagedObjectContext:_managedObjectContext];

detail1.cartID=[NSNumber numberWithInt:2000];

detail1.productID=[NSNumber numberWithInt:12];

detail1.productName=@"iphone5";

detail1.price=[NSDecimalNumber decimalNumberWithString:@"4000"];

ShoppingCartDetail *detail2=[NSEntityDescription insertNewObjectForEntityForName:@"ShoppingCartDetail" inManagedObjectContext:_managedObjectContext];

detail2.cartID=[NSNumber numberWithInt:2000];

detail2.productID=[NSNumber numberWithInt:23];

detail2.productName=@"iphone6";

detail2.price=[NSDecimalNumber decimalNumberWithString:@"5000"];

customer.cart=master;

[master addDetailObject:detail1];

[master addDetailObject:detail2];

//如果用户对数据做了修改

if([_managedObjectContext hasChanges]){

//进行持久化存储到文件中

BOOL ret=[_managedObjectContext save:nil];

if(ret){

NSLog(@"add successed!");

}

}

}

//显示数据

-(void)displayAllData

{

//进行数据查询请求的对象,参数指要查询的模型实体名

NSFetchRequest *request=[[NSFetchRequest alloc]initWithEntityName:@"Customer"];

NSString *name=@"Qianfeng";

//设置谓词对象(可以用于对数据记录筛查)

NSPredicate *predicate;

//精确匹配查找

//predicate=[NSPredicate predicateWithFormat:@"userName=%@",name];

//模糊查找,查询以name中的字符串开始,不区分大小写的记录

predicate=[NSPredicate predicateWithFormat:@"userName like [cd] %@",[NSString stringWithFormat:@"%@*",name]];

//设置请求中的谓词对象(筛查条件)

//request.predicate=predicate;

//在被管理对象上下文中执行查询请求,返回该模型对象数组

NSArray *result=[ _managedObjectContext executeFetchRequest:request error:nil];

for(Customer *cus in result){

//获取其cart属性

ShoppingCartMaster *master=cus.cart;

NSLog(@"customerID:%@,userName:%@,password:%@,cartID:%@",cus.customerID,cus.userName,cus.password,master.cartID);

//获取其详情,NSSet类型,转换为NSArray

NSArray *array=[master.detail allObjects];

//显示该customer的cart中所有的product

for(ShoppingCartDetail *detail in array){

NSLog(@"pid:%@,pname:%@,price:%@",detail.productID,detail.productName,detail.price);

}

}

}

-(void)updateData

{

NSFetchRequest *request=[[NSFetchRequest alloc]initWithEntityName:@"Customer"];

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"customerID=2"];

request.predicate=predicate;

//查询出需要修改的对象

NSArray * result=[_managedObjectContext executeFetchRequest:request error:nil];

if(result.count>0){

//取出需要修改的对象,直接对其属性值进行修改

Customer *customer=result[0];

customer.userName=@"lisi";

if([_managedObjectContext hasChanges]){

if([_managedObjectContext save:nil]){

NSLog(@"update ok!");

}

}

}else{

NSLog(@"没有找到需要修改的记录!");

}

}

//根据id删除某条记录

-(void)deleteDataByID:(int)cid

{

//查找要删除的记录对象

NSFetchRequest *request=[[NSFetchRequest alloc]initWithEntityName:@"Customer"];

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"customerID=%d",cid];

request.predicate=predicate;

NSArray *array=[_managedObjectContext executeFetchRequest:request error:nil];

if(array.count>0){

//取出要删除的对象

Customer *customer=array[0];

//在当前被管理对象上下文中删除对象

[_managedObjectContext deleteObject:customer];

if([_managedObjectContext hasChanges]){

if([_managedObjectContext save:nil]){

NSLog(@"delete successed!");

}else{

NSLog(@"delete error!");

}

}

}else{

NSLog(@"没有找到要删除的对象");

}

}