码迷,mamicode.com
首页 > 其他好文 > 详细

Objective-c CoreData

时间:2014-09-13 00:46:14      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   for   2014   div   

#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

标签:des   style   blog   color   io   ar   for   2014   div   

原文地址:http://www.cnblogs.com/wangwenfei/p/CoreData.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!