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
时间: 2024-10-14 13:13:29

Objective-c CoreData的相关文章

Objective-C

1.Objective-C语言特性 2.static __block const 3.Object-C的内存管理 4.RunLoop 5.iOS消息传递机制 6.iOS程序生命周期 7.MVC设计模式MVVM 8.UIView CALayer Frame 与bounds 9.根类 NSObject 10.多线程简介 11.数据持久化 12.JSON和XML HTML 自定义报文 13.网络编程 HTTP TCP/IP Socket  ASI AFNetwork 14.AppStore上传 及远程

coredata 的基本使用(转载)

CoreData提供了一种简便的对象持久化管理方法,使你可以不用关心数据的存储,只需要关心对象的增加.删除.更改.读写. 基本概念托管对象(managed object)一个托管对象代表你想要保存到数据存储中的一个对象.这在概念上类似于SQL中的一条记录, 并且通常也包含一些域,这些域对应于你想要保存的对象的属性. 数据存储(data store)Core Data支持4中类型的数据存储:SQLiteStore, XMLStore, BinaryStore, InMemoryStore. 托管对

Playground 你不知道的小技巧, CoreData 的使用

Playground 的出现无疑是大大的提高了开发效率,可以节省大量的编译时间. 这里介绍在 Playground 中使用 CoreData 的小技巧. 我们新建一个工程 iOS 项目工程. 点击 File -> New -> File , 在工程中新建文件 Data Model 文件  在 model 中添加一个 Entitle,如下图  编译工程后,在 Product 选择生成的 .app 文件,找到该目录,如下图  查看包中的文件,如图  可以看到一个 Mode.momd 文件, 如图 

IOS中CoreData浅析

CoreData简介: 什么是CoreData? Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数据操作期间,我们不需要编写任何SQL语句 CoreData 不能执行SQL语句 取而代之,操作的是对象.而常用的三方库 FMDB SQLite 可以直接SQL语句 CoreData和数据库有什么区别? CoreData是一个苹果原生的框架,它拥有像数

C++ vs Objective C

oc Short list of some of the major differences: C++ allows multiple inheritance, Objective-C doesn't.一个允许多继承,一个不允许Unlike C++, Objective-C allows method parameters to be named and the method signature includes only the names and types of the parameter

CoreData数据库

CoreData与Sqlite的比较 Sqlite:1.基于C接口,需要使用sql语句,代码繁琐 2.在处理大量数据时,表关系更直观 3.在OC中不是可视化的 CoreData:1.可视化,有undo/redo能力 2.可以实现多种文件格式NSSQLiteStoreType.NSBinaryStoreType.NSInMemoryStoreType,NSXMLStoreType 3.苹果官方API支持,与iOS结合更紧密 CoreData核心类关系: NSManagedObjectContext

Qt for iOS,Qt 与Objective C混合编程

项目设置 既然要聊 Qt 混合 OC 编程,首先要简单介绍一下 Objective C .我只有一句话:Go,问搜索引擎去.因为我所知实在有限,怕误导了您.当然如果您不怕,往下看吧. OC源文件介绍 首先我要说一下 Objective C 的源文件,后缀是.m 或 .mm ,在 .mm 文件里,可以直接使用 C++ 代码.所以,我们要混合 Qt 代码与 OC 代码,就需要在 Qt 项目里加入 mm 文件. pro 文件配置 Qt SDK for Mac ,安装之后, Qt Creator 会使用

CoreData __ 基本原理

操作过程 Context想要获取值,先要告诉连接器,我要什么东西 链接器再告诉store, 你给我什么东西, store去找 找到之后返回给链接器,链接器再返回给Context          CoreData和sqlite的区别 CoreData是一个框架;sqlite是苹果使用别人开发好的一个动态库,本质是关系型数据库. CoreData是IOS平台下的一个数据持久化的方式;sqlite可以跨平台使用. 实现思路 首先找到CoreData文件夹 创建Person类,并且建立name属性 C

K-means: optimization objective(最小化cost function来求相应的参数)

类似于linear regression,K-means算法也optimization objective或者是试图使cost function求最小值. 了解K-means算法的optimization objective有助于我们(1)调试算法时,看算法是否运行正确(在本节中可以看到)(2)使算法找到更好的cluster,避免局部最优解(在下节中会讲) K-means optimization objective uc(i):表示x(i)分给的那个cluster的cluster centro

CoreData

CoreData 是什么? Core Data 是一个模型层的技术.Core Data 帮助你建立代表程序状态的模型层.Core Data 也是一种持久化技术,它能将模型对象的状态持久化到磁盘,但它最重要的特点是:Core Data 不仅是一个加载.保存数据的框架,它还能和内存中的数据很好的共事 Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数据