Objective-c CoreData
#import "AppDelegate.h"
#import "Person.h"
@implementation AppDelegate @synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self createNewPersonWithFirstName:@"Anthony" lastName:@"Robbins" age:51];
[self createNewPersonWithFirstName:@"Richard" lastName:@"Branson" age:61]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Person"]; NSSortDescriptor *ageSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES]; NSSortDescriptor *firstNameSort = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES]; fetchRequest.sortDescriptors = @[ageSort, firstNameSort]; NSError *requstError = nil; NSArray *persons = [self.managedObjectContext executeFetchRequest:fetchRequest error:&requstError]; for (Person *person in persons) {
NSLog(@"First Name = %@", person.firstName);
NSLog(@"Last Name= %@", person.lastName);
NSLog(@"Age = %lu", (unsigned long)[person.age unsignedCharValue]);
} self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
} -(BOOL) deletePerson:(NSArray*)praramPersons
{
BOOL isDelete = NO; if ([praramPersons count] > 0) {
Person *lastPerson = [praramPersons lastObject];
[self.managedObjectContext deleteObject:lastPerson]; NSError *savingError = nil;
if ([self.managedObjectContext save:&savingError]) {
NSLog(@"Successfully deleted the last Person in the array");
} else {
NSLog(@"Failed to delete the last Person in the array");
}
} else {
NSLog(@"Could not find any Person entities in the context");
} return isDelete;
} -(BOOL) createPersonSuccess:(NSArray*)paramPersons
{
BOOL createResult = NO; if ([paramPersons count] > 0) {
createResult = YES;
NSUInteger counter = 1;
for (Person *thisPerson in paramPersons) {
NSLog(@"Person %lu First Name = %@", (unsigned long)counter, thisPerson.firstName);
NSLog(@"Person %lu lastName = %@", (unsigned long)counter, thisPerson.lastName);
NSLog(@"Person %lu Age = %ld", (unsigned long)counter, (unsigned long)[thisPerson.age unsignedIntegerValue]);
counter ++;
}
} else {
NSLog(@"Could not find any Person entities in the context");
} return createResult;
} -(void)createNewPerson
{
Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext]; if (newPerson != nil) {
newPerson.firstName = @"Anthony";
newPerson.lastName = @"Robbins";
newPerson.age = @51; NSError *savingError = nil; if ([self.managedObjectContext save:&savingError]) {
NSLog(@"Successfully saved the context.");
} else {
NSLog(@"Failed to save the context. Error = %@", savingError);
}
} else {
NSLog(@"Failed to create the new Person.");
} } -(BOOL) createNewPersonWithFirstName:(NSString*)paramFirstName
lastName:(NSString*)paramLastName
age:(NSUInteger)paramAge
{
BOOL result = NO; if ([paramFirstName length] == 0 | [paramLastName length] == 0) {
NSLog(@"First and Last names are mandatory");
return NO;
} Person *newPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext]; if (newPerson == nil) {
NSLog(@"Failed to create the new person.");
return NO;
} newPerson.firstName = paramFirstName;
newPerson.lastName = paramLastName;
newPerson.age = @(paramAge); NSError *savingError = nil; if ([self.managedObjectContext save:&savingError]) {
return YES;
} else {
NSLog(@"Failed to save the new person.Error = %@", savingError);
}
return result;
}
NSlog
2014-09-13 00:20:18.452 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.453 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.454 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.454 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.454 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.455 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.455 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.455 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.456 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.456 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.456 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.457 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.457 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.457 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.458 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.458 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.458 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.459 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.459 CoreDataDemo[683:60b] First Name = Anthony
2014-09-13 00:20:18.485 CoreDataDemo[683:60b] Last Name= Robbins
2014-09-13 00:20:18.486 CoreDataDemo[683:60b] Age = 51
2014-09-13 00:20:18.487 CoreDataDemo[683:60b] First Name = Richard
2014-09-13 00:20:18.487 CoreDataDemo[683:60b] Last Name= Branson
2014-09-13 00:20:18.487 CoreDataDemo[683:60b] Age = 61
2014-09-13 00:20:18.488 CoreDataDemo[683:60b] First Name = Richard
2014-09-13 00:20:18.488 CoreDataDemo[683:60b] Last Name= Branson
2014-09-13 00:20:18.489 CoreDataDemo[683:60b] Age = 61
2014-09-13 00:20:18.489 CoreDataDemo[683:60b] First Name = Richard
2014-09-13 00:20:18.489 CoreDataDemo[683:60b] Last Name= Branson
2014-09-13 00:20:18.490 CoreDataDemo[683:60b] Age = 61
2014-09-13 00:20:18.490 CoreDataDemo[683:60b] First Name = Richard
2014-09-13 00:20:18.491 CoreDataDemo[683:60b] Last Name= Branson
2014-09-13 00:20:18.491 CoreDataDemo[683:60b] Age = 61
2014-09-13 00:20:18.495 CoreDataDemo[683:60b] Application windows are expected to have a root view controller at the end of application launch
Objective-c CoreData的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- CoreData教程
网上关于CoreData的教程能搜到不少,但很多都是点到即止,真正实用的部分都没有讲到,而基本不需要的地方又讲了太多,所以我打算根据我的使用情况写这么一篇实用教程.内容将包括:创建entity.创建r ...
- iphone dev 入门实例4:CoreData入门
The iPhone Core Data Example Application The application developed in this chapter will take the for ...
- iOS基本数据库存储方式 - CoreData
CoreData 创建模型文件的过程 1.选择模板 2.添加实体 3.添加实体的属性[注意]属性的首字母必须小写 一.CoreData管理类(必备以下三个类对象) 1.CoreData数据操作的上下文 ...
- iOS CoreData 中 objectID 的不变性
关于 CoreData的 objectID 官方文档有这样的表述:新建的Object还没保存到持久化存储上,那么它的objectID是临时id,而保存之后,就是持久化的id,不会再变化了. 那么,我想 ...
- CoreData __ 基本原理
操作过程 Context想要获取值,先要告诉连接器,我要什么东西 链接器再告诉store, 你给我什么东西, store去找 找到之后返回给链接器,链接器再返回给Context Co ...
- iOS CoreData primitive accessor
Given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstNa ...
- 初识CoreData与详解
Core Data数据持久化是对SQLite的一个升级,它是iOS集成的,在说Core Data之前,我们先说说在CoreData中使用的几个类. (1)NSManagedObjectModel(被管 ...
- CoreData和SQLite多线程访问时的线程安全
关于CoreData和SQLite多线程访问时的线程安全问题 数据库读取操作一般都是多线程访问的.在对数据进行读取时,我们要保证其当前状态不能被修改,即读取时加锁,否则就会出现数据错误混乱.IOS中常 ...
- IOS数据存储之CoreData使用优缺点
前言: 学习了Sqlite数据之后认真思考了一下,对于已经习惯使用orm数据库的开发者或者对sql语句小白的开发者来说该如何做好数据库开发呢?这个上网搜了一下?看来总李多虑了!apple 提供了一种数 ...
随机推荐
- SQL效率的几点心得
这几天一直在写SQL,有时候对比同样效果的SQL语句,可是查询所需要的时间有时候相差很多,下面总结遇到的几个点: 1.between and 在有些时候自己比较喜欢使用这个语句,因为可以通过把数据 ...
- ARPA
ARPA是英文Advanced Research Projects Agency的缩写,代表美国国防部高级研究计划署.是美国国防部高级研究计划管理局因军事目的而建立的,开始时只连接了4台主机,这便是只 ...
- 微信/QQ机器人的实现
介绍: Mojo-Webqq和Mojo-Weixin是在github上基于webQQ和网页版WeiXin,用Perl语言实现的开源的客户端框架,它通过插件提供基于HTTP协议的api接口供其他语言或系 ...
- Jquery post 传递数组给asp.net mvc方法
以批量删除数据为例 做批量删除会需要传递要删除的数据ID数组 function RemoveLog(){ var postModel=[]; //遍历复选框获取要删除的数据ID 存放到数组中 $( ...
- List<T>中Exists 和Contains的区别
.net编码中,使用泛型List<>时,经常遇到这样的需求:新来一个Model对象,如果已有的List中没有这条数据,则把新对象Add到List中,否则不处理 判断已有的List中是否包含 ...
- 把NodeJS注册成Windows服务
为了让NodeJS开发的网站能在服务器端正常运行,最好把NodeJS需要运行的代码注册成Windows服务,服务器如果重启也不需要重新去启动NodeJS. 1.编写一个执行NodeJS脚本的bat文件 ...
- NodeJS - Express 3.0下ejs模板使用 partial展现 片段视图
如果你也在看Node.js开发指南,如果你也在一步一步实现 microBlog 项目!也许你会遇到本文提到的问题,如果你用的是Express 3.0 本书实例背景是 Express 2.0 而如今升级 ...
- 常见的装置与其在Linux当中的档名
需要特别留意的是硬盘机(不论是IDE/SCSI/U盘都一样),每个磁碟机的磁盘分区(partition)不同时, 其磁碟档名还会改变呢!下一小节我们会介绍磁盘分区的相关概念啦!需要特别注意的是磁带机的 ...
- EXT--columnWidth
在EXT 3.4API上没有查询到columnWidth这个配置项,但它却实实在在的在起作用,后来在ColumnLayout类查到它的信息: 上面的信息描述了采用了columnLayout布局的子面板 ...
- 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...