`
miss大为
  • 浏览: 80461 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Core Data数据持久化的使用

阅读更多

CoreData 是ios中用来对数据做持久化的一个框架,它对sqlite进行了封装,使我们不需要学习数据库知识,也不要写SQL语句就能将数据保存到数据库。下面来介绍CoreData的如何使用。

 

1. 新建一个项目,勾选使用Core Data, 新建后需要导入:CoreData.framework

 

 

2.新建项目后,AppDelegate类会生成三个属性

 

  1. @property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;  
  2. @property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;  
  3. @property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;  

 

这三个对象操作数据会用的到。

3.新建一个实体对象

 

4. 创建一个类与实体对象关联


5. 保存一个实体模型对象

 

[java] view plaincopy
  1. Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];  
  2. person.personId = @10002;  
  3. person.age = @29;  
  4. person.name = [NSString stringWithFormat:@"jack"];  
  5.   
  6. NSError *error = nil;  
  7. BOOL ret = [self.managedObjectContext save:&error];  
  8. if (ret) {  
  9.     NSLog(@"保存成功!");  
  10. else {  
  11.     NSLog(@"error");  
  12. }  

6.删除一个实体模型对象

 

 

  1. //删除  
  2. - (void)modifyPerson {  
  3.     NSEntityDescription *entifyDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];  
  4.     NSFetchRequest *fetchReqeust = [[NSFetchRequest alloc] init];  
  5.     [fetchReqeust setEntity:entifyDesc];  
  6.       
  7.     //查询年龄大于30的实体person对象  
  8.     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.age=30"];  
  9.     [fetchReqeust setPredicate:predicate];  
  10.     //查询出来的person对象数组  
  11.     NSArray *persons = [self.managedObjectContext executeFetchRequest:fetchReqeust error:nil];  
  12.     //遍历删除  
  13.     for (Person *p in persons) {  
  14.         [self.managedObjectContext deleteObject:p];  
  15.     }  
  16.       
  17. }  

7. 查询

 

 

  1. //根据条件查询数据  
  2. - (void)queryPerson {  
  3.     NSEntityDescription *entifyDesc = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];  
  4.     //查询对象  
  5.     NSFetchRequest *fetchReqeust = [[NSFetchRequest alloc] init];  
  6.     [fetchReqeust setEntity:entifyDesc];  
  7.       
  8.     //查询条件  
  9.     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.age>40 && self.personId>10015"];  
  10.     [fetchReqeust setPredicate:predicate];  
  11.       
  12.     //排序,按age降序排列  
  13.     NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];  
  14.     [fetchReqeust setSortDescriptors:@[sortDesc]];  
  15.       
  16.     //查询  
  17.     NSArray *persons = [self.managedObjectContext executeFetchRequest:fetchReqeust error:nil];  
  18.     for (Person *p in persons) {  
  19.         NSLog(@"name=%@,age=%@,id=%@",p.name,p.age,p.personId);  
  20.     }  
  21. }  


参考资料:
Core Data Reference API listing for the Core Data classes
http://developer.apple.com/documentation/Cocoa/Reference/CoreData_ObjC/index.html

NSPredicate Reference API listing for NSPredicate
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSPredicate.html

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics