core data的使用

//@synthesize name = _name;//当同时写了getter和setter方法之后,这个就失效了
//属性中,如果是给属性赋值,是一定要用self.,如果是调用属性,不要用self.,这样效率会高一些(注:只有是lazyload的使用,才使用self.)
/**
 *  Entity:实体,其实就是sqlite表
 Attribute:属性,就是sqlite中的字段
 Relationship:关系,就是sqlite中的表与表之间的关系
 Fetch Properties:查询属性,就是SQL语句
 
 .sqlite 数据库
 .sqlite-shm:数据库日志
 .sqlite-wal:数据库索引共享内存文件
 */


//@dynamic 动态的,与synthesize区别:dynamic是在运行(Runtime)时创建getter和setter方法,synthesize是在编译的时候创建getter和setter方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //打印沙河总目录
    NSLog(@"%@", NSHomeDirectory());
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    //调用方法
    //添加
    [self addCat];
    //查询所有
    [self allCats];
    //查询一个
    [self oneCat];
    
    return YES;
}
//coredata数据持久化的增删改查
//增
- (void)addCat
{
    //找到实体Cat,(找表)
    NSEntityDescription *catED = [NSEntityDescription entityForName:@"Cat" inManagedObjectContext:self.managedObjectContext];
    Cat *cat = [[Cat alloc] initWithEntity:catED insertIntoManagedObjectContext:self.managedObjectContext];
    cat.name = @"miaomiao";
    cat.age = @4;
    cat.breed = @"天猫";
    //加到临时数据库,如果没有错误,再添加到真实数据库
    
    //浅拷贝和深拷贝
    Cat *acat = [Cat copy];//浅拷贝,只拷贝地址(指针)
    acat = [cat mutableCopy];//深拷贝,拷贝指针和指针指向的内容
    
    
    
    NSError *error = nil;
    BOOL result = [self.managedObjectContext save:&error];
    if (result) {
        NSLog(@"添加成功");//若无错误,自动添加到真实数据库
    } else {
        NSLog(@"%@", error);
    }
}

//查询所有的Cat
- (void)allCats
{
    //相当于sqlite语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Cat"];
    
    NSError *error = nil;
    
    //查询并获取结果
    NSArray *catArr = [self.managedObjectContext executeFetchRequest:request error:&error];
    
    if (error) {
        NSLog(@"%@", error);
    } else {
        NSLog(@"查询成功");
        for (Cat *cat in catArr) {
            NSLog(@"%@", cat);
        }
    }
}

//查询一个
- (void)oneCat
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Cat"];
    
    //添加一些条件(谓词)
    //单引号
    request.predicate = [NSPredicate predicateWithFormat:@"name = 'mimi'"];
    //request.predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        
    //}];
    NSError *error = nil;
    NSArray *catArr = [self.managedObjectContext executeFetchRequest:request error:&error];
    if (error) {
        NSLog(@"%@", error);
    } else {
        NSLog(@"条件查询成功");
        for (Cat *cat in catArr) {
            
            NSLog(@"%@", cat);
            
            //先便利查询到,再修改
            [self update:cat];
            NSLog(@"%@", cat);
            
            //删除
            [self deleteCat:cat];
            NSLog(@"/////////%@", catArr);
        }
    }
}

//修改
- (void)update:(Cat *)cat
{
    cat.age = @10;
    NSError *error = nil;
    BOOL result = [self.managedObjectContext save:&error];
    if (result) {
        NSLog(@"修改成功");
    } else {
        NSLog(@"%@", error);
    }
}

//删除
- (void)deleteCat:(Cat *)cat
{
    [self.managedObjectContext deleteObject:cat];
    
    //保存
    NSError *error = nil;
    BOOL result = [self.managedObjectContext save:&error];
    if (result) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"%@", error);
    }
}

 
  

你可能感兴趣的:(数据持久化)