iOS进阶(coreData)

1.CoreData核心对象

实体管理类 NSManageredObject 实体描述类NSEntityDescription 数据管理类NSManageredObjectContext 数据连接器类NSPersistentStoreCoorDinator 数据模型器类NSManageredObjectmodel

2.利用CoreData对数据进行增删改查等操作

注意在创建工程时要勾选coreData

增删改都是建立在查询基础上的

创建查询请求

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];//查询到的是数据库中的所有内容

NSFetchRequest *request = [[NSFetchRequest alloc] init];//可以添加谓词和排序

创建实体

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.managedObjectContext];

request.entity = entity;//给请求添加实体

创建谓词

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@""];
    request.predicate = predicate;
    NSArray *array = [self.managedObjectContext executeFetchRequest:request error:nil];
    Student *stu = [array objectAtIndex:0];
    NSLog(@"name is %@, gender is %@, age is %@",stu.name, stu.gender, stu.age);

添加排序

NSSortDescriptor *des = [[NSSortDescriptor alloc] initWithKey:@"gender" ascending:YES];
    request.sortDescriptors = @[des];
原文地址:https://www.cnblogs.com/w150385/p/5251744.html