CoreData的学习
第一步:创建项目是勾选coredata,当然创建的时候没有勾选,之后还可以手动生产,

然后:创建数据库模型,及为其添加模型的属性。
然后生成模型文件:
注意⚠️:首先设置为Manual/None 不然在编译的过程中也会自动生成模型文件 造成编译失败

模型文件生成后,不用做修改,
生成模型文件后,就可以使用了。
现在就开始上代码:
#import "ViewController.h"
#import "AppDelegate.h"
#import "Person+CoreDataClass.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataSource;
@property (nonatomic, strong) AppDelegate * myAppdelegate; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 初始化数据源数组
self.dataSource = [NSMutableArray array]; self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
self.tableView.rowHeight = ; UIButton *add = [UIButton buttonWithType:UIButtonTypeContactAdd];
[add addTarget:self action:@selector(clickAdd) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithCustomView:add];
self.navigationItem.rightBarButtonItem = rightitem; AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
_myAppdelegate = delegate ;
NSManagedObjectContext *context = _myAppdelegate.persistentContainer.viewContext; // 进入控制器时 查询数据库数据
/**
两种方式创建request
*/
// NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
NSFetchRequest *request = [Person fetchRequest];
/**
数据排序
*/
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
request.sortDescriptors = @[descriptor];
/**
刷选数据
*/
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age == 8"];
// request.predicate = predicate;
NSError *error = nil;
NSArray *result = [context executeFetchRequest:request error:&error];
for (Person * p in result) {
NSLog(@"-查询结果:-----name:%@-------age:%lld",p.name,p.age);
}
[self.dataSource addObjectsFromArray:result];
[self.tableView reloadData];
} /**
插入数据
*/
- (void)clickAdd
{
NSManagedObjectContext *context = _myAppdelegate.persistentContainer.viewContext;
Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
person.name = @"jeck";
NSInteger temp = random() % + ;
person.age = temp;
[self.dataSource addObject:person];
[self.tableView reloadData];
[_myAppdelegate saveContext];
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSource.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
Person *modle = self.dataSource[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@----%lld",modle.name,modle.age];
return cell;
} - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
} /**
删除数据
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
Person *p = self.dataSource[indexPath.row];
[self.dataSource removeObject:p];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[_myAppdelegate.persistentContainer.viewContext deleteObject:p];
[_myAppdelegate saveContext];
}
} /**
点击修改数据
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Person *p = self.dataSource[indexPath.row];
p.name = @"rose";
[_myAppdelegate saveContext];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
简单说一下数据库的升级,模型版本的迁移:(能够使老版本的数据库正常使用)

新生成的新数据库文件:

切换之后,文件上勾选的是test_coreDate2了,此时就可以向新的模型库里添加新增的模型属性了,
然后需要生成一个模型的映射文件:
command + N



之后就会生成一个 v1.0_Tov1.1Model.xcmappingmodel 文件,前面的名字自己随意;
然后,就把旧的数据库的四个模型文件(Person+CoreDataClass.h ...m Person+CoreDataProperties.h ....m)删除,重新使用新的数据库test_CoreData2 创建模型文件。
看网上说的还需要修改Appdelegate中的代码,发现现在不用修改了。(想修改也找不到地方了,现在自动生成的代码和之前的不一样了)
在网上很多都是旧的,只发现这个比较新点http://blog.csdn.net/willluckysmile/article/details/76464249
CoreData的学习的更多相关文章
- iOS CoreData技术学习资源汇总
一.CoreData学习指引 1. 苹果官方:Core Data Programming Guide 什么是CoreData? 创建托管对象模型 初始化Core Data堆栈 提取对象 创建和修改自定 ...
- Core Data (一)备
序 恩,用Core Data也有一段时间了.大大小小的坑也都坑过了.重来没有认真的记录一次.这次需要好好的理一理Core Data.就当一次绝好的机会记录下来.也为了自己加深认识. 为什么要用Core ...
- masonry使用问题
2015年11月3日 coreData的学习练习中复习使用masonry自动布局 masonry自动布局发现问题: 两个控件的相对布局: 如果被参考对象用这个带anchor的属性,就会报这样一个错误: ...
- FireFox插件SQLite Manager的使用
最近几天开始高IOS数据库来着,一开始就CoreData的学习,结果高了一天没有一点进展. 没法,还是先老实代码着吧,不过用的火狐插件可视化数据库的操作也是不错的似乎. FireFox 插件:SQLi ...
- 【原】iOS学习之SQLite和CoreData数据库的比较
1. SQLite数据库 sqlite数据库操作的基本流程是, 创建数据库, 再通过定义一些字段来定义表格结构, 可以利用sql语句向表格中插入记录, 删除记录, 修改记录, 表格之间也可以建立联系. ...
- IOS学习:ios中的数据持久化初级(文件、xml、json、sqlite、CoreData)
IOS学习:ios中的数据持久化初级(文件.xml.json.sqlite.CoreData) 分类: ios开发学习2013-05-30 10:03 2316人阅读 评论(2) 收藏 举报 iOSX ...
- 学习在创建好的工程里面添加CoreData
在学习CoreData中, 在建工程后, 没有添加, 于是就参考网络文章进行更改. 这几天在学习做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项 ...
- CoreData学习-最好的一片文章
CoreData学习-最好的一片文章 分类: IOS重新上路2014-05-25 18:00 1937人阅读 评论(0) 收藏 举报 目录(?)[+] 写的很好的一篇教程,我什么时候能写出这么 ...
- CoreData的使用(IOS学习)
——杂言:最近开始学习IOS7的开发,下文是在已经建好的项目里加入CoreData的结构,并实现一个基于coredata的简单save,query. 1. 引入Core Data Framework. ...
随机推荐
- n阶乘,位数,log函数,斯特林公式
一.log函数 头文件: #include <math.h> 使用: 引入#include<cmath> 以e为底:log(exp(n)) 以10为底:log10(n) 以m为 ...
- php数组·的方法1-数组的操作
//range() 创建一个含指定范围的数组 可设置间隔 var_dump(range(1, 9, 2)); echo '<br>'; //array_combine() 合并两个数组创建 ...
- Python Numpy Array
Numpy 是Python中数据科学中的核心组件,它给我们提供了多维度高性能数组对象. Arrays Numpy.array dtype 变量 dtype变量,用来存放数据类型, 创建数组时可以同 ...
- vue中methods函数调用methods函数写法
export default { data() { return { hello:"你好" } }, methods:{ open(that) { that.hello = &qu ...
- 转发 django 初探
https://www.cnblogs.com/franknihao/p/7682914.html https://blog.csdn.net/tang_jin2015/article/details ...
- SQL事务的四种隔离级别
1未提交读(Read uncommitted):完全不锁表,所以会出现脏数据.2提交读(Read committed):1.事务1中update才锁表,可以select到最新数据. 事务2select ...
- PlayMaker Action的执行顺序
如图:默认的是从上到下 先执行Play Sound,再执行Destroy Object. 可以点击右上角的齿轮,也就是设置按钮选中Action Sequence,这样就会同时执行.
- python3+Appium自动化10-日志收集
日志概述 日志作用 日志是定位问题的重要手段 日志级别 级别 何时使用 DEBUG 调试信息,也是最详细的日志信息 INFO 证明事情按预期工作 WARNING 表明发生了一些意外,或者不就的将来(如 ...
- OpenStack Weekly Rank 2015.07.27
Module Reviews Drafted Blueprints Completed Blueprints Filed Bugs Resolved Bugs Cinder 7 1 1 7 10 Sw ...
- AWS的redhat7中安装jdk8
下载jdk8 wget https://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/ ...