• 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
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    jquery each循环遍历完再执行的方法
    PHP判断数组下标有没有存在的方法
    mysql General error: 1366 Incorrect string value: 'xF0x9Fx91x8DxF0x9F...' for column 'dianpumiaoshu' at row 1 解决方法
    jquery手指触摸滑动放大图片的方法(比较靠谱的方法)
    php swoole异步处理mysql
    php Yaf_Loader::import引入文件报错的解决方法
    PHP yaf显示错误提示
    PHP实现开发者模式出现该公众号提供的服务出现故障 请稍后再试解决方法
    css3 input placeholder颜色修改方法
    PHP获取PHP执行的时间
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5458408.html
Copyright © 2020-2023  润新知