zoukankan      html  css  js  c++  java
  • CoreData基本使用-01-CoreData

    //
    //  ViewController.m
    //  01-CoreData基本使用
    //
    //  Created by mac on 16/5/4.
    //  Copyright © 2016年 mac. All rights reserved.
    //
    
    #import "ViewController.h"
    #import <CoreData/CoreData.h>
    #import "User.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController {
        
        NSManagedObjectContext *_managerObjectContext;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self openDataBase];
        [self addUser];
        [self searchUserWithUserID:55];
        
        NSLog(@"---------%@", NSHomeDirectory());
    }
    
    /**
     *  1. 打开数据库
     */
    - (void)openDataBase {
        
        //1. 创建数据库文件
        NSString *dataBaseFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/User.sqlite"];
        NSURL *dataBaseFileUrl = [NSURL fileURLWithPath:dataBaseFilePath];
        
        //2. 创建模型描述文件
        /** a. 创建模型描述文件 b. */
        
        //3. 获取模型描述文件路径
        NSURL *entityFileURL = [[NSBundle mainBundle] URLForResource:@"UserModel" withExtension:@"momd"];
        NSLog(@"%@", entityFileURL);
        
        //4. 通过模型描述文件 来创建pcs(持久化存储坐标)
        
        NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:entityFileURL];// model
        NSPersistentStoreCoordinator *pcs = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
        
        //5.创建实体数据库文件
        NSError *error = nil;
        [pcs addPersistentStoreWithType:NSSQLiteStoreType configuration:NULL URL:dataBaseFileUrl options:nil error:&error];
        if (error) {
            NSLog(@"失败");
        } else {
            
            NSLog(@"成功");
        }
        
        _managerObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        _managerObjectContext.persistentStoreCoordinator = pcs;
    }
    /**
     *  2. 添加用户对象
     */
    - (void)addUser {
        
      /*  //1. 创建MO对象并且添加到objectContext
    //    User *user1 = [[User alloc] init];
        User *user1 = (User *)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_managerObjectContext];
        
        //2. 使用context 将数据存储到数据库
        user1.username = @"zhangsan";
        user1.password = @"1234";
        user1.user_id = @1001;
        
        //3. 使用context将数据存储到数据库
        NSError *error = nil;
        [_managerObjectContext save:&error];
        if (error) {
            NSLog(@"保存失败%@", error);
        } else {
            
            NSLog(@"保存成功");
        } */
        
        
        for (int i=0; i<100; i++) {
            
            User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_managerObjectContext];
            
            user.username = [NSString stringWithFormat:@"user%i", i];
            user.user_id = @(i);
            user.password = @"123456";
            
            //使用context将数据存储到数据库
            NSError *error = nil;
            [_managerObjectContext save:&error];
            if (error) {
                NSLog(@"保存失败");
            } else {
                NSLog(@"保存成功");
            }
        }
        
    }
    /**
     *  3. 查询
     */
    - (void)searchUserWithUserID:(NSInteger)userID {
        
        //1. 构建查询请求
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"User"];
        
        //2. 设置查询的条件
        //3. 使用谓词来设定查询的条件
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user_id>%li",userID];
        fetchRequest.predicate = predicate;
        
        //4. 执行查询请求
        NSError *error = nil;
        NSArray *resultArray = [_managerObjectContext executeFetchRequest:fetchRequest error:&error];
        
        //5. 处理查询结果
        if (error) {
            NSLog(@"查询出错%@", error);
        } else {
            
            //查询成功
            for (User *user in resultArray)
                NSLog(@"name = %@, password = %@, user_id = %@", user.username, user.password, user.user_id);
            }
    }
    
    @end
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    常见数据结构使用场景
    Java与算法之(4)
    Java与算法之(3)
    Java与算法之(2)
    Java与算法之(1)
    Maven适配多种运行环境的打包方案
    从头开始基于Maven搭建SpringMVC+Mybatis项目(4)
    从头开始基于Maven搭建SpringMVC+Mybatis项目(3)
    从头开始基于Maven搭建SpringMVC+Mybatis项目(2)
    从头开始基于Maven搭建SpringMVC+Mybatis项目(1)
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5458408.html
Copyright © 2011-2022 走看看