//
// ViewController.m
// XMCoreData
//
// Created by Floating_SH on 15/12/28.
// Copyright © 2015年 SH. All rights reserved.
//
#import "ViewController.h"
// 第一步引入頭文件
#import <CoreData/CoreData.h>
#import "Employee.h"
@interface ViewController ()
@property (nonatomic,strong) NSManagedObjectContext *context;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 配置協調器, 它連接三部分
// 協調器->數據模型
// 協調器->數據庫
// 協調器->上下文
// 協調器->模型
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
// 協調器->上下文
// 創建一個上下文對象
self.context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
// 該上下文對象的協調器指向我們剛纔創建的協調器
self.context.persistentStoreCoordinator = persistentStoreCoordinator;
// 協調器與數據庫
NSString *DocumentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *dataBasePath = [DocumentPath stringByAppendingFormat:@"XMCoreData.sqlite"];
[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dataBasePath] options:nil error:nil];
NSLog(@"%@",dataBasePath);
}
// 增加一個新員工
- (IBAction)insertAction:(id)sender {
Employee *emp = [NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:self.context];
emp.name = @"張三";
emp.height = @170;
emp.birthday = [NSDate date];
[self.context save:nil];
}
// 查
- (IBAction)selectAction:(id)sender {
// 先填寫一份查詢表
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Employee"];
// 填寫查詢條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"1=1"];
request.predicate = predicate;
// 排序條件
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"birthday" ascending:YES]];
// 執行請求返回結果.
NSArray *array = [self.context executeFetchRequest:request error:nil];
for (Employee *emp in array) {
NSLog(@"name = %@,height = %@,birthday = %@",emp.name,emp.height,emp.birthday);
}
}
// 改
- (IBAction)updateAction:(id)sender {
// 查詢.(fetch)
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
// Specify criteria for filtering which objects to fetch
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"1=1"];
[fetchRequest setPredicate:predicate];
// Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthday"
ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
NSError *error = nil;
NSArray *fetchedObjects = [self.context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
//
NSLog(@"空");
}
// 修改fetchedObjects即可
for (Employee *emp in fetchedObjects) {
emp.name = @"李四";
}
// 保存上下文
[self.context save:nil];
}
// 刪
- (IBAction)deleteAction:(id)sender {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
// Specify criteria for filtering which objects to fetch
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"1=1"];
[fetchRequest setPredicate:predicate];
// Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthday"
ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
NSError *error = nil;
NSArray *fetchedObjects = [self.context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
//
}
// 開始刪除
for (Employee *emp in fetchedObjects) {
[self.context deleteObject:emp];
}
[self.context save:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// Employee+CoreDataProperties.h
// XMCoreData
//
// Created by Floating_SH on 15/12/28.
// Copyright © 2015年 SH. All rights reserved.
//
// Choose "Create NSManagedObject Subclass…" from the Core Data editor menu
// to delete and recreate this implementation file for your updated model.
//
#import "Employee.h"
NS_ASSUME_NONNULL_BEGIN
@interface Employee (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *name;
@property (nullable, nonatomic, retain) NSNumber *height;
@property (nullable, nonatomic, retain) NSDate *birthday;
@end
NS_ASSUME_NONNULL_END
//
// Employee+CoreDataProperties.m
// XMCoreData
//
// Created by Floating_SH on 15/12/28.
// Copyright © 2015年 SH. All rights reserved.
//
// Choose "Create NSManagedObject Subclass…" from the Core Data editor menu
// to delete and recreate this implementation file for your updated model.
//
#import "Employee+CoreDataProperties.h"
@implementation Employee (CoreDataProperties)
@dynamic name;
@dynamic height;
@dynamic birthday;
@end