码迷,mamicode.com
首页 > 其他好文 > 详细

CoreData __ 基本原理

时间:2016-03-30 23:46:49      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

操作过程

 

  • Context想要获取值,先要告诉连接器,我要什么东西
  • 链接器再告诉store, 你给我什么东西, store去找
  • 找到之后返回给链接器,链接器再返回给Context               

 

CoreData和sqlite的区别

  • CoreData是一个框架;sqlite是苹果使用别人开发好的一个动态库,本质是关系型数据库.

  • CoreData是IOS平台下的一个数据持久化的方式;sqlite可以跨平台使用.

实现思路

  •   首先找到CoreData文件夹
  •   创建Person类,并且建立name属性

技术分享

技术分享

 

Command + N 创建

技术分享

 

//代码实现

 

-------ViewController.h

 

#import "ViewController.h"
//coreData的框架
//导入框架的方式 : <框架名字/头文件名字>
#import <CoreData/CoreData.h>
#import "Person.h"
#import "AppDelegate.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
//临时数据库
@property (nonatomic, strong) NSManagedObjectContext *objectContext;
//数据源数组
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.dataArray = [NSMutableArray array];
    //获取到我们最开始使用的AppDelegate
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    self.objectContext = app.managedObjectContext;
    
    //调用查询数据的方法
    [self searchAll];
    
    _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(barButtonItemClicked)];
    

}


- (void)barButtonItemClicked
{
    //NSEntityDescription : 实体描述类,通过类方法创建
    //第一个参数 : 表示这个实体描述类描述的是哪个实体 (表名)
    //第二个参数 : 表示的是在context里边创建一个描述,告诉context我要往里面插入一个object了
    NSEntityDescription *description = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.objectContext];
    //创建一个实体类
    //第一个参数 : 实体描述
    //第二个参数 : 在context里面放入这个类
    Person *person = [[Person alloc] initWithEntity:description insertIntoManagedObjectContext:self.objectContext];
    
    int number = arc4random() % 2000;
    person.name = [NSString stringWithFormat:@"%d号李四", number];
    
    [self insertObject:person];
    
    
}

//向coreData中插入一条数据
- (void)insertObject:(Person *)person
{
    
    NSError *error = nil;
    //把context保存到本地
    //这个方法执行之后, 本地数据才发生了改变
    [self.objectContext save:&error];
    
    if (error == nil) {
        [self.dataArray addObject:person];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dataArray.count - 1 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
    }
}

//搜索全部
- (void)searchAll
{
    //创建一个查询操作, 查询哪个表里面的内容
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
    
    //接收查询数据
    NSError *error = nil;
    //让context去执行查询操作, 并且返回一个结果数组
    NSArray *array = [self.objectContext executeFetchRequest:request error:&error];
    
    //判断error是否为nul
    if (error == nil) {
        [self.dataArray setArray:array];
        
        [self.tableView reloadData];
    }else
    {
        NSLog(@"查询失败 ");
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //判断一下当前的编辑状态
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //获取到想要删除的那条数据
        Person *person = self.dataArray[indexPath.row];
        //在context中将这条数据删除
        [self.objectContext deleteObject:person];
        
        NSError *error = nil;
        
        [self.objectContext save:&error];
        
        if (error == nil) {
            //先删数据
            [self.dataArray removeObject:person];
            //然后更新UI
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //知道要改谁
    Person *person = self.dataArray[indexPath.row];
    
    int number = arc4random() % 2000;
    person.name = [NSString stringWithFormat:@"%d号张三", number];
    
    NSError *error = nil;
    
    [self.objectContext save:&error];

    if (error == nil) {
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"forIndexPath:indexPath];
    Person *person = self.dataArray[indexPath.row];
    cell.textLabel.text = person.name;
    
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

CoreData __ 基本原理

标签:

原文地址:http://www.cnblogs.com/guosir/p/5339187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!