- 以前简单学习过coredata,但是在公司这么时间不用了,又感觉快忘记了,今天就拿点时间出来复习下coredata
- 本文只是简单的复习下coredata,涉及到的内容也只是数据库的增删改查这些基本操作
用coredata必须知道的几个概念
/*
1 、NSManagedObjectContext 管理对象,上下文,持久性存储模型对象
2、NSManagedObjectModel 被管理的数据模型,数据结构
3、NSPersistentStoreCoordinator 连接数据库的
4、NSManagedObject 被管理的数据记录
5、NSFetchRequest 数据请求
6、NSEntityDescription 表格实体结构
*/
//
// ViewController.m
// CoreData1
//
// Created by liyang on 16/5/18.
// Copyright © 2016年 liyang. All rights reserved.
//
#import "ViewController.h"
#import "AppDelegate.h"
#import <CoreData/CoreData.h>
#import "Person.h"
@interface ViewController ()
/** appdelegate */
@property (nonatomic, strong) AppDelegate *myAppDelegate;
/** 创建一个上下文的管理对象 */
@property (nonatomic, strong) NSManagedObjectContext *myContext;
/** 数据源数组 */
@property (nonatomic, strong) NSMutableArray *allDataArray;
@end
@implementation ViewController
/*
1 、NSManagedObjectContext 管理对象,上下文,持久性存储模型对象
2、NSManagedObjectModel 被管理的数据模型,数据结构
3、NSPersistentStoreCoordinator 连接数据库的
4、NSManagedObject 被管理的数据记录
5、NSFetchRequest 数据请求
6、NSEntityDescription 表格实体结构
*/
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化
self.myAppDelegate = [UIApplication sharedApplication].delegate;
self.myContext = self.myAppDelegate.managedObjectContext;
self.allDataArray = [NSMutableArray array];
// 删除所有的数据
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
NSArray *arr = [self.myContext executeFetchRequest:request error:nil];
for (Person *per in arr) {
[self.myContext deleteObject:per];
}
[self.myAppDelegate saveContext];
}
#pragma mark - 增
- (IBAction)btn1:(id)sender {
// 创建一个实体描述对象
NSEntityDescription *personDes = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.myContext];
// 创建多个数据
for (int i = 0; i < 10; i++) {
// 创建一个Person对象
Person *person = [[Person alloc] initWithEntity:personDes insertIntoManagedObjectContext:self.myContext];
person.name = [NSString stringWithFormat:@"liyang--%d", i];
person.age = [NSNumber numberWithInt:i];
[self.myContext insertObject:person];
// 加到数组中
[self.allDataArray addObject:person];
}
[self.myAppDelegate saveContext];
}
#pragma mark - 查
- (IBAction)btn2:(id)sender {
// 查询表中数据
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
// 排序
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
[request setSortDescriptors:@[sort]];
// 按条件查询
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age = %@", @23];
// [request setPredicate:predicate];
NSError *error;
// 查询,注意返回的结果是数组
NSArray *arr = [self.myContext executeFetchRequest:request error:&error];
if (!error) {
for (Person *per in arr) {
NSLog(@" %@, %@", per.age, per.name);
}
}
}
#pragma mark - 改
- (IBAction)btn3:(id)sender {
Person *per = [self.allDataArray objectAtIndex:0];
if (per) {
per.name = @"superMan";
per.age = @1;
[self.myAppDelegate saveContext];
}
}
#pragma mark - 删
- (IBAction)btn4:(id)sender {
Person *per = [self.allDataArray lastObject];
if (per) {
[self.myContext deleteObject:per];
[self.allDataArray removeObject:per];
[self.myAppDelegate saveContext];
}
}
@end
-
在创建工程的时候记得勾选coredata,不然自己从新建还是挺麻烦的
-
经常忘记在表中建的实体怎么导出,这其实就是
创建子类.pngcom+N
,选择coredata,建个子类,然后全部勾选,下一步,全部勾选,下一步的事情
-
如上图
网友评论