Core Data

来源:互联网 发布:mac eclipse gbk 编辑:程序博客网 时间:2024/06/13 04:42

CoreData 核心数据;在MVC中M是数据模型,平常我们使用的时候,都是创建一个继承于NSObject的Model,创建对应的属性;而CoreData是属于系统的一种数据管理系统

数据管理一般都是增删改查,我是直接使用storyBoard创建了一个表视图控制器,创建了一个表视图,将数据展示到表视图中,下面我将自己所写的代码贴出来,代码有些乱,但是都实现了功能:

#pragma mark 添加一个班级

- (IBAction)addClass:(id)sender {

    

    //1.创建一个班级对象 (增删改查中的增)

    

    //创建一个视图对象的描述 得到实体()是什么样的结构

    NSEntityDescription *classED = [NSEntityDescriptionentityForName:@"Entity"inManagedObjectContext:self.context];

    //接着创建一个班级对象,这个对象继承于NAManagerdObject;并且把这条数据保存到当前上下文(一个暂时的数据库)

    Entity *class = [[Entityalloc]initWithEntity:classEDinsertIntoManagedObjectContext:self.context];

    

    //赋值  使用UIImageJPEGRepresentation这个函数将图片转换成NSData

    //图片赋值

    class.headImage =UIImageJPEGRepresentation([UIImageimageNamed:@"8.jpg"],1.0);

    int num = arc4random() %1000 +1000;

    class.name = [NSStringstringWithFormat:@"XAS15 %d",num];

    NSArray *cityArr =@[@"北京",@"上海",@"大连",@"郑州",@"广州",@"杭州",@"西安"];

    int index = arc4random() %6;

    class.city = cityArr[index];

    class.createTime = [NSDatedate];

    

    //2.将此保存到文件

    NSError *error ;

    if (!error) {

        [self.contextsave:&error];

          //3.添加到数据源

        [self.datasourceaddObject:class];

            //4.添加到UI界面

        //生成最后一行的下标

        NSIndexPath *indexPath = [NSIndexPathindexPathForRow:self.datasource.count -1 inSection:0];

        //将数据添加到列表的最后一行

        [self.tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];

        //tableView滑动到添加到一行

        [self.tableViewscrollToRowAtIndexPath:indexPathatScrollPosition:UITableViewScrollPositionMiddleanimated:YES];

    }

}



#pragma mark ------------ 增删改查中的查

- (void)readData{

    //创建请求对象

    NSFetchRequest *request = [NSFetchRequestfetchRequestWithEntityName:@"CustomModel"];

    //可以设置过滤条件coreData中的一个属性

    NSPredicate *pre = [NSPredicatepredicateWithFormat:@"city = %@",@"大连"];

    request.predicate = pre;

    

    NSSortDescriptor *sort = [NSSortDescriptorsortDescriptorWithKey:@"city"ascending:YES];

    request.sortDescriptors = @[sort]; //这里是一个数组

    NSArray *classes = [self.contextexecuteFetchRequest:requesterror:nil];

    [self.datasourcesetArray:classes];

    

}


#pragma mark - ------- 修改

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //设置修改条件 (把大连的city修改成西安)


    NSFetchRequest *request = [NSFetchRequestfetchRequestWithEntityName:@"Entity"];

    

    NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"city = %@",@"郑州"];

    request.predicate = predicate;

    NSSortDescriptor *sortDescriptior = [NSSortDescriptorsortDescriptorWithKey:@"city"ascending:YES];

    request.sortDescriptors = @[sortDescriptior];

    NSArray *classes = [self.contextexecuteFetchRequest:requesterror:nil];

    

    //查询完成之后开始修改

    //遍历查询返回的数组

    for (Entity *classin classes) {

        class.city = @"西安";

    }

    NSError *error;

    [self.contextsave:&error];

    [self.tableViewreloadData];

    

}




由于表视图有自己的删除方法,所以我直接就在这个方法中进行数据的删除:


#pragma mark ----------- 表视图自带删除功能 所以在这个自带方法中进行删除操作

// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        //删除数据库中的数据

        CustomModel *model = self.datasource[indexPath.row];

        [self.contextdeleteObject:model];

        

        //暂时数据源同步到本地数据源

        NSError *error;

        [self.contextsave:&error];

        //移除数据源中的数据

        if (!error) {

            [self.datasourceremoveObject:model];

            [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];

        }

} elseif (editingStyle ==UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

    }   

}

这里涉及的只是coreData的一对一的数据管理,较为简单;

请多多指教~







0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝发脾气摔东西躺地上怎么办 生气拿棍子打了孩子怎么办 小宝宝被蚊虫咬了怎么办 小宝宝被蚊子咬了怎么办 衣架打小孩淤青怎么办 1岁宝宝有痰咳嗽怎么办 1岁宝宝咳嗽有痰怎么办 孩子爱动手打家长怎么办 不小心有了孩子该怎么办 2岁多宝宝干咳怎么办 2岁宝宝咳嗽无痰怎么办 打了孩子后悔了怎么办 12小孩脾气很犟怎么办 被学生气着了怎么办 1岁宝宝轻微咳嗽怎么办 4岁宝宝突然呕吐怎么办 4岁儿童突然呕吐怎么办 四岁儿童90身高怎么办 24个月宝宝缺钙怎么办 狗狗总是要人陪着玩怎么办 成年了还是很皮怎么办 三岁儿子太调皮怎么办 10个月宝宝粘人怎么办 6个月宝宝粘人怎么办 9个月宝宝偏矮怎么办 1岁宝宝粘人爱哭怎么办 宝宝2岁半胆小怎么办 5岁宝宝超级粘人怎么办 狗狗吃饭要人喂怎么办 十个月宝宝认人怎么办 一岁宝宝粘人怎么办 9个月宝宝粘妈妈怎么办 一岁的宝宝呕吐怎么办 宝宝一岁八个月太粘人了怎么办 六个月的宝宝好粘人怎么办 两岁半宝宝说话突然结巴了怎么办 1岁宝宝突然呕吐怎么办 宝宝吃坏了呕吐怎么办 1岁宝宝吃饭爱玩怎么办 7岁儿童半夜呕吐怎么办 一个月宝宝粘人怎么办