CoreDate的使用
勾选 xcode的 CoreDate会帮我们自动创建 CoreData 但是我们通常不那样使用,通常把 CoreDate 在单利类中创建,
//
// ZYDAO.h
// StoryboardTest
//
// Created by wanglixing on 15/11/27.
// Copyright © 2015年 zzz. All rights reserved.
// #import <Foundation/Foundation.h>
#import "People.h" @interface ZYDAO : NSObject + (instancetype)sharedDAO; - (People* )insertPeopleWithName:(NSString* )name phone:(NSString* )phone; - (NSArray* )selectAllPeople; - (void)updatePeople; - (void)deletePeople:(People* )people; @end
//
// ZYDAO.m
// StoryboardTest
//
// Created by wanglixing on 15/11/27.
// Copyright © 2015年 zzz. All rights reserved.
// #import "ZYDAO.h"
@import CoreData; NSString* const PEOPLE = @"People"; @interface ZYDAO () {
NSManagedObjectModel* _model;
NSPersistentStoreCoordinator* _coordinator;
NSManagedObjectContext* _context;
} @end @implementation ZYDAO + (instancetype)sharedDAO {
static ZYDAO* dao = nil; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dao = [[ZYDAO alloc] init];
}); return dao;
} - (id)init {
if (self = [super init]) {
NSURL* modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]; _model = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; _coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:_model]; NSURL* sqliteURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"Model.sqlite"]; if (![_coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:sqliteURL options:nil error:nil]) {
NSLog(@"打开数据库失败");
} _context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; [_context setPersistentStoreCoordinator:_coordinator];
} return self;
} - (People* )insertPeopleWithName:(NSString* )name phone:(NSString* )phone {
People* people = [NSEntityDescription insertNewObjectForEntityForName:PEOPLE inManagedObjectContext:_context]; people.name = name;
people.phone = phone; //把修改同步到数据库。
[_context save:nil]; return people;
} - (NSArray* )selectAllPeople {
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:PEOPLE]; return [_context executeFetchRequest:request error:nil];
} - (void)updatePeople {
[_context save:nil];
} - (void)deletePeople:(People* )people {
[_context deleteObject:people]; [_context save:nil];
} @end
CoreDate的使用的更多相关文章
- coreDate 简化版
建表: 自动生成: 代码: // // RootViewController.m // coreDate 简化版 #import "RootViewController.h" #i ...
- iOS移动开发CoreDate讲解
----欢迎------- 在移动端开发,数据持久化保存是基本要素,没钱在2014年之后退出了coredate,本持久化基于oc作为开发,方便程序人员操作.与SQL数据库,MySQL相比,优点颇多. ...
- GitHub上值得关注的iOS开源项目
1.AFNetworking地址:https://github.com/AFNetworking/AFNetworking用于网络请求 2.JSONKit地址:https://github.com/j ...
- magento获取页面url的办法还有magento的常用函数
<?php echo $this->getStoreUrl('checkout/cart');?> 获取结账页面的url:<?php echo $this->getUrl ...
- 高级iOS开发工程师的面试题
1:CALayer与UIView的区别是什么? 两者最大的区别就是:涂层不会直接渲染到屏幕上: UIView是iOS界面元素的基础,所有界面元素都是继承于它,他的本身全是由CoreAnimation来 ...
- oc常见误区
1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作, 2.异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依然 ...
- iOS开发——面试指导
iOS面试指导 一 经过本人最近的面试和对面试资料的一些汇总,准备记录这些面试题,以便ios开发工程师找工作复习之用,本人希望有面试经验的同学能和我同时完成这个模块,先出面试题,然后会放出答案. 1. ...
- Core Data数据持久性存储基础教程-备用
摘要 就像我一直说的,Core Data是iOS编程,乃至Mac编程中使用持久性数据存储的最佳方式,本质上来说,Core Data使用的就是SQLite,但是通过一系列特性避免了使用SQL的一些列的麻 ...
- ios NSUserDefaults存储数据(偏好设置)
ios NSUserDefaults存储数据(偏好设置) 1.NSUserDefaults用于存储数据量小的数据,主要是用户配置,但也可以支持存储一些小数据包括:NSString, NSNumber, ...
随机推荐
- leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)
https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and ...
- HDU-4738 Caocao's Bridges 边联通分量
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:在有重边的无向图中,求权值最小的桥. 注意trick就好了,ans为0时输出1,总要有一个 ...
- Running a Remote Desktop on a Windows Azure Linux VM (远程桌面到Windows Azure Linux )-摘自网络(试了,没成功 - -!)
A complete click-by-click, step-by-step video of this article is available ...
- 第二百四十天 how can I 坚持
在家待了一天,晚上出去买了个帽子,还有买了点排骨炖着吃了... 玩了好多局游戏. 想搞个直播,不知道能不能玩的起来. 水平太菜了,明天去小米之家玩玩. 睡觉.
- 快速切换目录软件推荐——autojump
受到<autojump: 在命令行下快速更改目录>的鼓动,决定试用下这个软件. 但ubuntu下的源貌似有些问题, sudo apt get install autojump 后,死活提示 ...
- Linux下的内核测试工具——perf使用简介
Perf是Linux kernel自带的系统性能优化工具.Perf的优势在于与Linux Kernel的紧密结合,它可以最先应用到加入Kernel的new feature.pef可以用于查看热点函数, ...
- C#中ArrayList和string,string[]数组的转换
转载原地址: http://www.cnblogs.com/nextsoft/articles/2218689.html 1.ArrarList 转换为 string[] : ArrayList li ...
- freemaker获取字符串长度
freemarker 判断字符串长度大于多少或者int变量大于多少,比较<#if "test"?length gt 2> 长度大于2</#if> 大于 ...
- 11道php面试题
贡献11道php面试题及解决方法,跟大家总结一下曾经遇到的部分面试题.希望可以给大家得到帮助. 1. 什么事面向对象?主要特征是什么? 面象对象是把自然界的物体和概念直接映射到程序界的一种比较优雅的手 ...
- c/c++字符串处理大集合
rember this strncpy(a,b,5); a[5]='\0'; char a[10]; memset(a,'#',sizeof(a)); a[10]='\0'; 刚开始学C/C++时,一 ...