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

 

posted on 2016-05-04 14:53  爱你久久iOS  阅读(149)  评论(0编辑  收藏  举报

导航