CoreData

作者: 我爱吃豆芽 | 来源:发表于2016-06-24 19:34 被阅读128次

使用coreadata第一步便是coredata的创建.

123.gif
 //
//  ViewController.m
//  coredata
//
//  Created by zhaoguodong on 16/6/24.
//  Copyright © 2016年 zhaoguodong. All rights reserved.
//

#import "ViewController.h"
#import "STUDENT.h"
#import "TEACHER.h"


@interface ViewController ()


//联系上下文
@property (nonatomic ,strong)NSManagedObjectContext *context;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化  NSManagedObjectContext
    self.context = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSMainQueueConcurrencyType];
    
    //关联数据库
    NSManagedObjectModel *model = [[NSManagedObjectModel alloc]initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]];
    //通过文件模型去初始化数据持久化助理
    NSPersistentStoreCoordinator *store = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
    
    //创建文件路径
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"model.sqlite"];
    
    NSLog(@"%@",filePath);
    
    //关联路径
          //如果需要支持版本迭代的话那么要在options设置@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES}
            //如果不想迭代设置为options nil
    [store   addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:filePath] options:@{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES} error:nil];
    
    
    self.context.persistentStoreCoordinator  = store;
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark ---------coreDate的使用需要注意,所有的增删改操作完成之后需要保存一下---
#增
- (IBAction)add:(UIButton *)sender {
    
    //创建一个学生
    STUDENT *stu1 = [NSEntityDescription insertNewObjectForEntityForName:@"STUDENT" inManagedObjectContext:self.context];
    stu1.name = @"光头强";
    stu1.age = @26;
    
    
    STUDENT *stu2 = [NSEntityDescription insertNewObjectForEntityForName:@"STUDENT" inManagedObjectContext:self.context];
    stu2.name = @"🐻大";
    stu2.age = @6;
    
    
    STUDENT *stu3 = [NSEntityDescription insertNewObjectForEntityForName:@"STUDENT" inManagedObjectContext:self.context];
    stu3.name = @"🐻二";
    stu3.age = @4;
    
    STUDENT *stu4 = [NSEntityDescription insertNewObjectForEntityForName:@"STUDENT" inManagedObjectContext:self.context];
    stu4.name = @"图图";
    stu4.age = @1;
    
    
    //创建一个老师
    TEACHER *teac1 = [NSEntityDescription insertNewObjectForEntityForName:@"TEACHER" inManagedObjectContext:self.context];
    teac1.name = @"🐯大师";
    teac1.sex = @"雄性";

    //吧学生给老师
    teac1.myStudent =[NSSet setWithObjects:stu1,stu2,stu3,stu4, nil];
  
    [self.context save:nil];

}

#删
- (IBAction)delete:(id)sender {
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"TEACHER"];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@"🐯大师"];
    
    request.predicate = predicate;
    
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    
    for (STUDENT *stu in array) {
        [self.context deleteObject:stu];
        [self.context save:nil];
    }
}
#更新
- (IBAction)update:(UIButton *)sender {
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"STUDENT"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@"图图"];
    request.predicate = predicate;
    
    NSArray *array = [self.context executeFetchRequest:request error:nil];
    
    for (STUDENT *stu in array) {
        stu.name = @"吉吉";
        [self.context save:nil];
    }
    
}

#查找
- (IBAction)search:(UIButton *)sender {

    //查询老师表
    NSFetchRequest *request =[NSFetchRequest fetchRequestWithEntityName:@"TEACHER"];
    
       NSArray *array =  [self.context executeFetchRequest:request error:nil];
    
    for (TEACHER *teac in array) {
     
        for (STUDENT *stu in teac.myStudent) {
     //teac有个属性 myStudent  里面就是他的所有学生
            NSLog(@"STUDENT:name=%@  age=%@",stu.name,stu.age);
        }
        NSLog(@"TEACHER:name = %@ sex:%@",teac.name,teac.sex);
    }
    
    
}


@end


相关文章

网友评论

    本文标题:CoreData

    本文链接:https://www.haomeiwen.com/subject/zkwodttx.html