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]; }]; }