CoreData的使用

        右键新建文件选择CoreData中的CoreDataModel,在.xcdatamodeld文件中添加实体类,并为其添加属性,在Xcode右侧Entity属性栏里的class为实体管理对象起名字,之后右键新建文件选择CoreData下的NSManagedObject subclass自动生成类。之后新建工程勾选CoreData会在AppDelegate中生成代码,将其拷贝,新建文件CoreDataDAO粘贴进去,此类为所有数据访问对象的父类,自己新建数据访问对象可以定义增删改查得方法,另外自己也要定义一个不被管理的实体对象,属性与ManagedObject属性一致,此时文件有.xcdatamodeld文件ManagedObject文件CoreDataDAO文件和自己定义的实体文件共5个,在CoreDataDAO.m文件中修改 - ( NSManagedObjectModel *)managedObjectModel方法中的 [[ NSBundle mainBundle ] URLForResource : @"Note" withExtension : @"momd ]的名字与.xcdatamodeld一致, - ( NSPersistentStoreCoordinator *)persistentStoreCoordinator 中修改 NSURL *storeURL = [[ self applicationDocumentsDirectory ] URLByAppendingPathComponent : @"Note.sqlite ]行中为数据存储为数据库文件的名字。此时基本工作已经做好。
在子类化的数据访问对象里添加查找方法(无条件查询)
- ( NSMutableArray *)findAll{
获取上下文对象
    NSManagedObjectContext *cxt = [ self managedObjectContext ];
获取实体关联描述类
    NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
数据提取请求类用于查找
    NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
    [requestsetEntity:entityDescription];
排序描述类可以指定排序字段及排序方式
    NSSortDescriptor *sortDescriptor = [[ NSSortDescriptor alloc ] initWithKey : @"name" ascending : YES ];
    [request
setSortDescriptors : @[ sortDescriptor ] ];
   NSError *err = nil;
根据前面设置的请求对象查询返回数组
    NSArray *listData = [cxt executeFetchRequest :request error :&err];
   NSMutableArray *resListData = [[NSMutableArrayalloc]init];
遍历数组中的MO对象并将MO对象中的内容赋值于自己定义的实体类中
    for ( NoteManagedObject *mo in listData) {
       
Note *note = [[ Note alloc ] init ];
        note.
name = mo. name ;
        note.
age = [mo. age intValue ];
        note.
sex = mo. sex ;
        [resListData
addObject :note];
    }
   
return resListData;
}
有条件查询
此方法可以返回数组
- ( Note *)findById:( Note *)model{
   
NSManagedObjectContext *cxt = [ self managedObjectContext ];
   
NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
   
NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
    [requestsetEntity:entityDescription];
通过谓词限定查询条件
    NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"name = %@" ,model. name ];
    [request
setPredicate :predicate];
   NSError *err = nil;
查询返回数组
    NSArray *listData = [cxt executeFetchRequest :request error :&err];
//    NSMutableArray *resListData = [[NSMutableArray alloc]init];
//    for (NoteManagedObject *mo in listData) {
//        Note *note = [[Note alloc]init];
//       note.name = mo.name;
//        note.age = [mo.age intValue];
//        note.sex = mo.sex;
//        [resListData addObject:note];
//    }
   
if (listData. count > 0 ) {
       
NoteManagedObject *no = [listData lastObject ];
       
Note *note = [[ Note alloc ] init ];
        note.
name = no. name ;
        note.
age = [no. age intValue ];
        note.
sex = no. sex ;
       
       
return note;
    }
   
return nil ;
}
修改数据
插入
- ( int )create:( Note *)model{
   NSManagedObjectContext *cxt = [self managedObjectContext];
创建一个被管理的实体对象用于保存
    NoteManagedObject *note = [ NSEntityDescription insertNewObjectForEntityForName : @"Note" inManagedObjectContext :cxt];
    [note
setValue :model. name forKey : @"name" ];
    [note
setValue :[ NSNumber numberWithInteger :model. age ] forKey : @"age" ];
    note.
sex = model. sex ;
   
   NSError *error;
语句保存修改同步到数据库中
    if ([ self . managedObjectContext save :&error]) {
       
NSLog ( @" 数据库保存成功! " );
    }
else {
       
NSLog ( @" 数据插入失败! " );
       
return - 1 ;
    }
   
return 0 ;
}
删除
- ( int )remove:( Note *)model{
   
NSManagedObjectContext *cxt = [ self managedObjectContext ];
   
NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
   
NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
    [request
setEntity :entityDescription];
   
NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"name = %@" ,model. name ];
    [request
setPredicate :predicate];
   
NSError *error;
   
NSArray *listData = [cxt executeFetchRequest :request error :&error];
   
if (listData. count > 0 ) {
       NoteManagedObject *note = [listDatalastObject];
上下文对象调用deleteObject方法删除数据
        [ self . managedObjectContext deleteObject :note];
       NSError *savingError =nil;
删除后调用保存方法
        if ([ self . managedObjectContext save :&savingError]) {
           
NSLog ( @" 删除数据成功! " );
        }
else {
           
NSLog ( @" 删除数据失败! " );
           
return - 1 ;
        }
    }
   
return 0 ;
}
修改
- ( int )modify:( Note *)model{
   
NSManagedObjectContext *cxt = [ self managedObjectContext ];
   
NSEntityDescription *entityDescription = [ NSEntityDescription entityForName : @"Note" inManagedObjectContext :cxt];
   
NSFetchRequest *request = [[ NSFetchRequest alloc ] init ];
    [request
setEntity :entityDescription];
   
NSPredicate *predicate = [ NSPredicate predicateWithFormat : @"name = %@" ,model. name ];
    [request
setPredicate :predicate];
   
NSError *error;
   
NSArray *listData = [cxt executeFetchRequest :request error :&error];
   if (listData.count>0) {
修改对象的内容
        NoteManagedObject *mo = [listData lastObject ];
        mo.
name = model. name ;
        mo.
age = [ NSNumber numberWithInt :model. age ];
        mo.
sex = model. sex ;
       NSError *savingError =nil;
修改后保存
        if ([ self . managedObjectContext save :&savingError]) {
           
NSLog ( @" 修改数据成功! " );
        }
else {
           
NSLog ( @" 修改数据失败! " );
           
return - 1 ;
        }
    }
   
return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值