CoreData基础
CoreData用于做数据持久化,适合大数据量的存储和查询
CoreData不是数据库 CoreData可以使用数据库 ,XML等方式来存储数据
CoreData使用面向对象的方式操作数据
CoreData操作数据无需编写SQL语句
使用时 需要导入CoreData框架
//----------------------------------------------------------
NSManagedObjectContext 负责应用和数据库之间的交互
NSPersistentStoreCoordinator 添加持久化存储库(初始化后 给其初始化一个数据文件)

NSManagedObjectModel 被管理的对象模型
NSEntityDescription :实体描述 向实体中添加数据-->给context保管
#import <CoreData/CoreData.h>
#import "User.h"
#import "Movie.h"
@interface ViewController (){
NSManagedObjectContext *_context;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//打开数据库
[self openDataBase];
//添加数据
// [self addUser];
//查询数据
[self queryData];
//修改数据
// [self updateData];
//删除数据
[self deleteData];
NSLog(@"-------------------------");
[self queryData];
}
- (void)openDataBase{
//1.NSManagedObjectModel 加载数据模型文件
NSURL *url = [[NSBundle mainBundle]URLForResource:@"MyData" withExtension:@"momd"];
NSManagedObjectModel *objectModel = [[NSManagedObjectModel alloc]initWithContentsOfURL:url];
//2.创建 持久化存储库 带有(载入)数据模型文件
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:objectModel];
//持久化存储库 存放到路径
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/MyData.sqlite"];
NSLog(@"filePath : %@",filePath);
NSURL *url1 = [NSURL fileURLWithPath:filePath];
NSError *error = nil;
//3.设置 持久化存储库
[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url1 options:nil error:&error];
if (error) {
NSLog(@"开启失败");
}else{
NSLog(@"开启成功");
}
_context = [[NSManagedObjectContext alloc]init];
//context 中的数据存储到指定的 "psc" 中(一次指定后面都不会用psc了) -->后面所有操作对context操作
_context.persistentStoreCoordinator = psc;
}
- (void)addUser{
//添加 实体 数据---->向User实体中添加数据-->给context保管
User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:_context];
user.name = @"张杰";
user.age = @22;
user.height = @177;
Movie *movie = [NSEntityDescription insertNewObjectForEntityForName:@"Movie" inManagedObjectContext:_context];
movie.movieName = @"速度与激情";
movie.movieID = @1003;
movie.price = @152;
Movie *movie1 = [NSEntityDescription insertNewObjectForEntityForName:@"Movie" inManagedObjectContext:_context];
movie1.movieName = @"恋爱男女";
movie1.movieID = @1004;
movie1.price = @112;
//添加数据后保存数据
BOOL isSuccess = [_context save:nil];
if (isSuccess) {
NSLog(@"添加成功");
}else{
NSLog(@"添加失败");
}
}
- (void)queryData{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];
NSFetchRequest *request2 = [NSFetchRequest fetchRequestWithEntityName:@"Movie"];
// 在context中查询(context相当于容器)
NSArray *dataArr = [_context executeFetchRequest:request error:nil];
NSArray *arr2 = [_context executeFetchRequest:request2 error:nil];
for (User *model in dataArr) {
NSLog(@"%@ %@ %@",model.name,model.age,model.height);
}
for (Movie *model in arr2) {
NSLog(@"%@ %@ %@",model.movieName,model.movieID,model.price);
}
}
//修改数据
- (void)updateData{
//查询找到需要修改的数据
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Movie"];
//请求 设置请求条件
request.predicate = [NSPredicate predicateWithFormat:@"movieID = 1004"];
//查询-->结果
NSArray *dataArr = [_context executeFetchRequest:request error:nil];
//拿到查询到的数据
for (Movie *model in dataArr) {
//遍历出来 修改数据
model.movieName = @"大话西游";
}
//修改完成 保存
BOOL isSuccess = [_context save:nil];
if (isSuccess) {
NSLog(@"修改成功");
}else{
NSLog(@"修改失败");
}
}
- (void)deleteData{
//查询数据
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Movie"];
request.predicate = [NSPredicate predicateWithFormat:@"movieID=1004"];
NSArray *arr =[_context executeFetchRequest:request error:nil];
for (Movie *model in arr) {
//context删除数据
[_context deleteObject:model];
}
//删除数据后保存
[_context save:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
CoreData基础的更多相关文章
- coreData数据操作
// 1. 建立模型文件// 2. 建立CoreDataStack// 3. 设置AppDelegate 接着 // // CoreDataStack.swift // CoreDataStackDe ...
- 从Windows角度看Mac OS X上的软件开发
如果原来从事Windows软件开发,想跨足或转换至Mac OS X环境,需要知道那些东西?有什么知识技能可以快速运用在Mac OS X环境上的?这两个问题应该是Windows开发者进入Mac OS X ...
- iOS基础教程:在建好的项目中加入CoreData[转]
这几天在做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项目初期,没有包含进CoreData.于是就在已建好的项目中加入CoreData.由于第一 ...
- 优于CoreData的Realm数据库基础教程
Realm 是一个跨平台的移动数据库引擎,于 2014 年 7 月发布,准确来说,它是专门为移动应用所设计的数据持久化解决方案之一. Realm 可以轻松地移植到您的项目当中,并且绝大部分常用的功能( ...
- iOS基础 - 数据库CoreData
一.iOS应用数据存取的常用方式 XML属性列表 —— PList NSKeyedArchiver 归档 Preference(偏好设置) SQLite3 Core Data 二.Core Data简 ...
- Swift基础之CoreData的使用
以前使用过OC版本的CoreData应该很好理解Swift方式,所以这里简单的展示一下,增删改查的方法使用,同时给大家说一下创建步骤,方便大家的使用,转载请注明出处,谢谢~ 步骤一:创建一个Swift ...
- iOS-数据持久化-CoreData
CoreData详解 介绍: 在Cocoa环境下,如果你想使用数据库(如sqlite),你可以使用sql语句的方式通过相关的工具类进行数据库的直接操作.当然你也可以通过别人封装之后的一些简单框架,使得 ...
- 【原】iOS学习之SQLite和CoreData数据库的比较
1. SQLite数据库 sqlite数据库操作的基本流程是, 创建数据库, 再通过定义一些字段来定义表格结构, 可以利用sql语句向表格中插入记录, 删除记录, 修改记录, 表格之间也可以建立联系. ...
- MagicalRecord,一个简化CoreData操作的工具库
简介 项目主页:https://github.com/magicalpanda/MagicalRecord 实例下载:https://github.com/ios122/MagicalRecord 在 ...
随机推荐
- 【转】mysql存储引擎
http://www.cnblogs.com/kevingrace/p/5685355.html Mysql有两种存储引擎:InnoDB与Myisam,下表是两种引擎的简单对比 MyISAM In ...
- 启用与关闭 Ad Hoc Distributed Queries
在数据库里执行以下脚本: 启用: exec sp_configure 'show advanced options',1reconfigureexec sp_configure 'Ad Hoc Dis ...
- Android Studio使用org.apache.http报错
Android Studio使用org.apache.http报错需要加上这句话:useLibrary 'org.apache.http.legacy'
- 关于oracle 10g creating datafile with zero offset for aix
参考文档: 1.创建oracle数据文件时需要注意的地方(OS Header Block) http://www.aixchina.net/Question/20406 2.oracle 创建数据文件 ...
- selenium2(WebDriver)环境搭建
1.安装jdk并配置环境变量: jdk安装jdk下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html环境变量 ...
- windows下调用发送邮件程序项*发送邮件
#include <windows.h>int _tmain(int argc, _TCHAR* argv[]){ ShellExecute(NULL, _T("open&quo ...
- DDX_Text ()函数 C++
DDX_Text()函数管理着对话框.表格视或控件视对象中的编辑控件与对话框.表格视或控件视对象的CString型数据成员之间的int,UINT,long,DWORD,CString,float或do ...
- xcode8 上传ipa文件无法构建版本
在xcode8 升级后上传ipa文件 需要设置一个安全提示,现在上传app store的方式为xcode或者 application loader 一.xcode 准备工作完成后点击Product-- ...
- [12]APUE:高级 I/O
一.分散聚离(向量) I/O [a] readv / writev #include <sys/uio.h> ssize_t readv(int fd, const struct iove ...
- CentOS 6.5 x86_64系统手动释放内存
1.查询当前内存使用情况和释放缓存的参数 redismaster 10:29:24 [~] [root] free -m total used free shared buffers cachedMe ...