CoreData浅谈
2016-06-03 11:09
267 查看
什么是coredata?
第一、CoreData并不属于数据库,而是苹果公司封装的进行数据持久化的框架;
第二、CareData不仅支持SQLite文件,而且支持XML、二进制文件.
(一)插入数据
- (IBAction)addModel:(id)sender {
//创建实体描述对象
NSEntityDescription *description = [NSEntityDescriptionentityForName:@"Cloese"inManagedObjectContext:self.mydelegate.managedObjectContext];
//1.先创建模型对象
Cloese *cloese = [[Cloesealloc]
initWithEntity:descriptioninsertIntoManagedObjectContext:self.mydelegate.managedObjectContext];
cloese.name =@"Lucy";
int price =arc4random() %
100 +1;
cloese.price = [NSNumbernumberWithInt:price];
//插入数据源数组
[self.dataSourceaddObject:cloese];
[self.tableviewinsertRowsAtIndexPaths:@[[NSIndexPathindexPathForRow:self.dataSource.count
-1 inSection:0]]withRowAnimation:UITableViewRowAnimationLeft];
//对数据管理器中的更改进行存储
[self.mydelegatesaveContext];
}
(二)删除数据
//允许tableview可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
returnYES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle ==UITableViewCellEditingStyleDelete) {
//删除数据源
Cloese *cloese =self.dataSource[indexPath.row];
[self.dataSourceremoveObject:cloese];
//删除数据管理器的数据
[self.mydelegate.managedObjectContextdeleteObject:cloese];
//将进行的更改进行保存
[self.mydelegatesaveContext];
//删除单元格
[self.tableviewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
}
}
(三)修改数据
//点击cell的方法用来修改数据
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//先找到模型对象
Cloese *cloese =self.dataSource[indexPath.row];
cloese.name =@"小福子";
cloese.price = [NSNumbernumberWithInt:213];
[self.tableviewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
//将进行的更改进行保存
[self.mydelegatesaveContext];
}
(四)查询数据
- (void)viewDidLoad {
[superviewDidLoad];
self.dataSource = [NSMutableArrayarray];
self.mydelegate = [UIApplicationsharedApplication].delegate;
//查询数据
NSFetchRequest *request = [[NSFetchRequestalloc]
initWithEntityName:@"Cloese"];
NSSortDescriptor *sort = [[NSSortDescriptoralloc]
initWithKey:@"price"ascending:YES];
request.sortDescriptors =@[sort];
//执行查询
NSError *error =nil;
NSArray *result = [self.mydelegate.managedObjectContextexecuteFetchRequest:request
error:&error];
//给数据源数组中添加数据
[self.dataSourceaddObjectsFromArray:result];
}
第一、CoreData并不属于数据库,而是苹果公司封装的进行数据持久化的框架;
第二、CareData不仅支持SQLite文件,而且支持XML、二进制文件.
(一)插入数据
- (IBAction)addModel:(id)sender {
//创建实体描述对象
NSEntityDescription *description = [NSEntityDescriptionentityForName:@"Cloese"inManagedObjectContext:self.mydelegate.managedObjectContext];
//1.先创建模型对象
Cloese *cloese = [[Cloesealloc]
initWithEntity:descriptioninsertIntoManagedObjectContext:self.mydelegate.managedObjectContext];
cloese.name =@"Lucy";
int price =arc4random() %
100 +1;
cloese.price = [NSNumbernumberWithInt:price];
//插入数据源数组
[self.dataSourceaddObject:cloese];
[self.tableviewinsertRowsAtIndexPaths:@[[NSIndexPathindexPathForRow:self.dataSource.count
-1 inSection:0]]withRowAnimation:UITableViewRowAnimationLeft];
//对数据管理器中的更改进行存储
[self.mydelegatesaveContext];
}
(二)删除数据
//允许tableview可编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
returnYES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle ==UITableViewCellEditingStyleDelete) {
//删除数据源
Cloese *cloese =self.dataSource[indexPath.row];
[self.dataSourceremoveObject:cloese];
//删除数据管理器的数据
[self.mydelegate.managedObjectContextdeleteObject:cloese];
//将进行的更改进行保存
[self.mydelegatesaveContext];
//删除单元格
[self.tableviewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
}
}
(三)修改数据
//点击cell的方法用来修改数据
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//先找到模型对象
Cloese *cloese =self.dataSource[indexPath.row];
cloese.name =@"小福子";
cloese.price = [NSNumbernumberWithInt:213];
[self.tableviewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
//将进行的更改进行保存
[self.mydelegatesaveContext];
}
(四)查询数据
- (void)viewDidLoad {
[superviewDidLoad];
self.dataSource = [NSMutableArrayarray];
self.mydelegate = [UIApplicationsharedApplication].delegate;
//查询数据
NSFetchRequest *request = [[NSFetchRequestalloc]
initWithEntityName:@"Cloese"];
NSSortDescriptor *sort = [[NSSortDescriptoralloc]
initWithKey:@"price"ascending:YES];
request.sortDescriptors =@[sort];
//执行查询
NSError *error =nil;
NSArray *result = [self.mydelegate.managedObjectContextexecuteFetchRequest:request
error:&error];
//给数据源数组中添加数据
[self.dataSourceaddObjectsFromArray:result];
}
相关文章推荐
- Core Data
- CoreData
- coredata
- coreData
- CoreData取数据提示data:<fault>且数据为空解决办法
- iOS里CoreData的 增 删 改 查
- CoreData 多线程下NSManagedObjectContext的使用
- 如何结合CoreData给UITableView的section填充数据
- CoreData增删改查简单操作及多线程添加数据
- IOS之分析网易新闻存储数据(CoreData的使用,增删改查)
- core data学习笔记
- CoreData的基本操作过程
- 苹果的 CoreData
- Core Data 的学习与思考(一)
- IOS开发 - CoreData详解
- ios 动画与2D、3D绘图
- iOS教程:Core Data数据持久性存储基础教程
- iOS教程:如何使用Core Data – 预加载和引入数据
- IOS CoreData 多表查询(上)
- 使用FMDB