CoreData

//
//  AppDelegate.m
//  CoreDataLearnByCode
//
//  Created by lance on 13-12-31.
//  Copyright (c) 2013年 Lance. All rights reserved.
//

#import "AppDelegate.h"
#import "ProfessionViewController.h"
@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [_managedObjectContext release];
    [_persistentStoreCoordinator release];
    [_managedObjectModel release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    ProfessionViewController *professionViewController = [[ProfessionViewController alloc] initwithManagedObjectContext:self.managedObjectContext];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:professionViewController];
    [professionViewController release];
    self.window.rootViewController = nav;
    [nav release];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark coreData method

#pragma mark getter
// setp:1
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    //get the NSManagedObjectModel,
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataLearn" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}
// step:2
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    // mapp sqlite url
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataLearnByCode.sqlite"];
    NSError *error = nil;
    // by NSManagedObjectModel to init NSPersistentStoreCoordinator
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"%@:%@", error , [error userInfo]);
        abort();
    }
    return _persistentStoreCoordinator;
}
// step:3
- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectModel != nil) {
        return _managedObjectContext;
    }
    NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator;
    if (coordinator != nil) {
        // by NSPersistentStoreCoordinator to init NSManagedObjectContext;
        _managedObjectContext = [[NSManagedObjectContext alloc] init];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}
// step:4
- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        // by NSManagedObjectContect to save it.
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
            NSLog(@"unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

@end


model

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Student;

@interface Profession : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * cid;
@property (nonatomic, retain) NSSet *students;
@end

@interface Profession (CoreDataGeneratedAccessors)

- (void)addStudentsObject:(Student *)value;
- (void)removeStudentsObject:(Student *)value;
- (void)addStudents:(NSSet *)values;
- (void)removeStudents:(NSSet *)values;

@end

#import "Profession.h"
#import "Student.h"


@implementation Profession

@dynamic name;
@dynamic cid;
@dynamic students;

@end

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Profession;

@interface Student : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * sno;
@property (nonatomic, retain) Profession *profession;

@end

#import "Student.h"
#import "Profession.h"


@implementation Student

@dynamic name;
@dynamic sno;
@dynamic profession;

@end

View&Controller

Profession

#import <UIKit/UIKit.h>

@interface ProfessionViewController : UITableViewController <UIAlertViewDelegate>
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
- (id)initwithManagedObjectContext:(NSManagedObjectContext *)managedObjectContext;
@end

//
//  ProfessionViewController.m
//  CoreDataLearnByCode
//
//  Created by lance on 13-12-31.
//  Copyright (c) 2013年 Lance. All rights reserved.
//

#import "ProfessionViewController.h"
#import "Profession.h"
#import "StudentViewController.h"
@interface ProfessionViewController ()

@end

#define EntityName @"Profession"

@implementation ProfessionViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initwithManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        self.managedObjectContext = managedObjectContext;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configData];
    self.title = @"Profession";
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addProfession)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // update fetch
    [self fetchProfessions];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_managedObjectContext release];
    [_fetchedResultsController release];
    [super dealloc];
}

#pragma mark BarButton action
- (void)addProfession
{
    UIAlertView *inputAlert = [[UIAlertView alloc] initWithTitle:@"add" message:@"add a new Profession" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    inputAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [inputAlert show];
    [inputAlert release];
}

#pragma mark UIAlertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        // entity
        NSEntityDescription *professionDescription = [NSEntityDescription entityForName:EntityName inManagedObjectContext:self.managedObjectContext];
        // object
        Profession *newProfession = (Profession *)[[NSManagedObject alloc] initWithEntity:professionDescription insertIntoManagedObjectContext:self.managedObjectContext];
        newProfession.name = [alertView textFieldAtIndex:0].text;
        NSError *error = nil;
        if (![self.managedObjectContext save:&error]) {
            NSLog(@"save profession error :%@", error);
        }
        [self fetchProfessions];
        [self.tableView reloadData];
    }
}

#pragma mark config data
- (void)configData
{
    // fetchRequest to get objects
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:EntityName];
    // set fetchrequest property
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cid" ascending:YES];
    [fetchRequest setSortDescriptors:@[sortDescriptor]];
    // set cache
    NSString *cacheName = [EntityName stringByAppendingString:@"Cache"];
    // init fetchedResultsController
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:cacheName];
}

// get objects from db
- (void)fetchProfessions
{
    NSError *error = nil;
    BOOL success = [self.fetchedResultsController performFetch:&error];
    if (!success) {
        NSLog(@"failed to fetch data:%@", error);
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.fetchedResultsController.fetchedObjects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    Profession *profession = (Profession *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = profession.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"(%d)", [profession.students count]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        NSManagedObject *deleted = [self.fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
        [self.managedObjectContext deleteObject:deleted];
        NSError *error = nil;
        if (![self.managedObjectContext save:&error]) {
            NSLog(@"delete error :%@", error);
            abort();
        }
        [self fetchProfessions];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StudentViewController *studentViewController = [[StudentViewController alloc] initwithProfession:[self.fetchedResultsController objectAtIndexPath:indexPath]];
    [self.navigationController pushViewController:studentViewController animated:YES];
    [studentViewController release];
}

@end
Student
#import <UIKit/UIKit.h>
#import "Profession.h"
@interface StudentViewController : UITableViewController <UIAlertViewDelegate>

@property (nonatomic, retain) Profession *profession;
- (id)initwithProfession:(Profession *)profession;
@end

//
//  StudentViewController.m
//  CoreDataLearnByCode
//
//  Created by lance on 13-12-31.
//  Copyright (c) 2013年 Lance. All rights reserved.
//

#import "StudentViewController.h"
#import "Student.h"
@interface StudentViewController ()

@end
#define EntityName @"Student"
@implementation StudentViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initwithProfession:(Profession *)profession
{
    self = [super initWithStyle:UITableViewStylePlain];
    if (self) {
        self.profession = profession;
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = self.profession.name;
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addProfession)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark BarButton action
- (void)addProfession
{
    UIAlertView *inputAlert = [[UIAlertView alloc] initWithTitle:@"add" message:@"add a new Student" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    inputAlert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [inputAlert show];
    [inputAlert release];
}

#pragma mark UIAlertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        // entity
        NSEntityDescription *professionDescription = [NSEntityDescription entityForName:EntityName inManagedObjectContext:self.profession.managedObjectContext];
        // object
        Student *newStudent = (Student *)[[NSManagedObject alloc] initWithEntity:professionDescription insertIntoManagedObjectContext:self.profession.managedObjectContext];
        newStudent.name = [alertView textFieldAtIndex:0].text;
        NSError *error = nil;
        [self.profession addStudentsObject:newStudent];
        if (![self.profession.managedObjectContext save:&error]) {
            NSLog(@"save Student error :%@", error);
        }
        [self.tableView reloadData];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.profession.students count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    Student *student = (Student *)[self.profession.students.allObjects objectAtIndex:indexPath.row];
    cell.textLabel.text = student.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",student.sno];
    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        NSManagedObject *deleted = [self.profession.students.allObjects objectAtIndex:indexPath.row];
        [self.profession.managedObjectContext deleteObject:deleted];
        NSError *error = nil;
        if (![self.profession.managedObjectContext save:&error]) {
            NSLog(@"delete object failed :%@", error);
        }
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
     */
}

@end


源碼地址

https://github.com/Lance201314/CoreDataLearnByCode
發佈了65 篇原創文章 · 獲贊 7 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章