CoreData

//

//  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











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值