NSManagedObject:模型对象,或者叫实体(entity),可理解为OC⾥里的对象。数据库
NSManagedObjectModel:模型⽂文件(.xcdatamodeld),记录全部的模型对象及对象 的属性框架
NSPersistentStoreCoordinator:持久化存储协调者,是数据库和程序之间的桥梁, ⽤用来设置数据存储的名字,位置,存储⽅方式,和存储时机。⽤用来执⾏行具体的CRUD操做。fetch
NSManagedObjectContext:对象管理上下⽂文,⽤用户全部的持久化操做都是经过此 对象。再由此对象调NSPersistentStoreCoordinator对象执⾏行具体的操做。spa
#import "ViewController.h" #import "User+CoreDataProperties.h" #import "Book+CoreDataProperties.h" #import "AppDelegate.h" @interface ViewController () { AppDelegate *delegate; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%@",NSHomeDirectory()); delegate = [UIApplication sharedApplication].delegate; // delegate.managedObjectContext; // [self insertObject]; // [self updateObject]; // [self findObject]; [self deleteObject]; }
增删改查操做code
#pragma mark - 添加 - (void)insertObject { // 建立实体对象 // NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:delegate.managedObjectContext]; // 一、添加一个实体对象到 managedObjectContext 中 User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:delegate.managedObjectContext]; user.userID = @"10002"; user.userName = @"张三"; user.userAge = @28; user.userSex = @"男"; user.saveDate = [NSDate date]; // 二、保存上下文 [delegate saveContext]; }
#pragma mark - 查询 - (void)findObject { // 一、建立一个取值请求 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"]; // 使用谓词设置查询条件 // request.predicate = [NSPredicate predicateWithFormat:@"userAge < 35"]; // request.predicate = [NSPredicate predicateWithFormat:@"userName LIKE '*杨*'"]; // 设置查询的条数 request.fetchLimit = 2; // 指定到某个位置开始查询(分页效果) request.fetchOffset = 2; // 按某个字段升序、降序排列 NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"userAge" ascending:YES]; request.sortDescriptors = @[sort]; NSError *error = nil; // 二、在上下文中执行取值请求 NSArray *objects = [delegate.managedObjectContext executeFetchRequest:request error:&error]; [objects enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { User *user = (User *)obj; NSLog(@"%@ %@ %@ %@ %@",user.userID, user.userName, user.userAge, user.userSex, user.saveDate); }]; }
#pragma mark - 更新 - (void)updateObject { // 一、查询 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"]; request.predicate = [NSPredicate predicateWithFormat:@"userID == '10005'"]; NSArray *objects = [delegate.managedObjectContext executeFetchRequest:request error:nil]; [objects enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // 二、修改 User *user = (User *)obj; user.saveDate = [NSDate date]; // 三、保存 [delegate saveContext]; }]; }
#pragma mark - 删除 - (void)deleteObject { // 一、查询 NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"]; request.predicate = [NSPredicate predicateWithFormat:@"userID == '10005'"]; NSArray *objects = [delegate.managedObjectContext executeFetchRequest:request error:nil]; [objects enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { // 二、删除 [delegate.managedObjectContext deleteObject:obj]; // 三、保存 [delegate saveContext]; }]; }