iOS.데이터 영구화.PersistenceLayer.Core Data

19984 단어 Core Data
#import <Foundation/Foundation.h>

#import <CoreData/CoreData.h>



@interface Persistence04NoteCoreDataDAO : NSObject





// 

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;

// 

@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;

// 

@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;



- (NSURL *)applicationDocumentsDirectory;



@end

 
#import "Persistence04NoteCoreDataDAO.h"



@implementation Persistence04NoteCoreDataDAO



@synthesize managedObjectContext = _managedObjectContext;

@synthesize managedObjectModel = _managedObjectModel;

@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;





#pragma mark - Core Data  

//   

- (NSManagedObjectContext *)managedObjectContext

{

    if (_managedObjectContext) {

        return _managedObjectContext;

    }

    

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

    if (coordinator) {

        _managedObjectContext = [[NSManagedObjectContext alloc] init];

        [_managedObjectContext setPersistentStoreCoordinator:coordinator];

    }

    return _managedObjectContext;

}



//    

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

{

    if (_persistentStoreCoordinator) {

        return _persistentStoreCoordinator;

    }

    

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataNotes.sqlite"];

    

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    

    [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType

                      configuration:nil

                                URL:storeURL

                            options:nil

                              error:nil];

    

    return _persistentStoreCoordinator;

}



//     

- (NSManagedObjectModel *)managedObjectModel

{

    if (_managedObjectModel) {

        return _managedObjectModel;

    }

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataNotes" withExtension:@"momd"];

    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];

    return _managedObjectModel;

}



#pragma mark -  

//  Docment NSURL 

- (NSURL *)applicationDocumentsDirectory

{

    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];

}



@end

 
#import <CoreData/CoreData.h>



#import "Persistence04NoteCoreDataDAO.h"

#import "Persistence04Note.h"

#import "NoteManagedObject.h"



@interface Persistence04NoteDAO : Persistence04NoteCoreDataDAO





+ (Persistence04NoteDAO*)sharedManager;



// Note 

-(int) create:(Persistence04Note*)model;



// Note 

-(int) remove:(Persistence04Note*)model;



// Note 

-(int) modify:(Persistence04Note*)model;



// 

-(NSMutableArray*) findAll;



// 

-(Persistence04Note*) findById:(Persistence04Note*)model;



@end

 
#import "Persistence04NoteDAO.h"



@implementation Persistence04NoteDAO



static Persistence04NoteDAO *sharedManager = nil;



+ (Persistence04NoteDAO*)sharedManager

{

    static dispatch_once_t once;

    dispatch_once(&once, ^{

        

        sharedManager = [[self alloc] init];

        [sharedManager managedObjectContext];

        

    });

    return sharedManager;

}





// Note 

-(int) create:(Persistence04Note*)model

{

    

    NSManagedObjectContext *cxt = [self managedObjectContext];

    

    NoteManagedObject *note = [NSEntityDescription insertNewObjectForEntityForName:@"Persistence04Note" inManagedObjectContext:cxt];

    [note setValue: model.content forKey:@"content"];

    [note setValue: model.date forKey:@"date"];

    

    note.date = model.date;

    note.content = model.content;

    

    NSError *savingError = nil;

    if ([self.managedObjectContext save:&savingError]){

        NSLog(@" ");

    } else {

        NSLog(@" ");

        return -1;

    }

    

    return 0;

}



// Note 

-(int) remove:(Persistence04Note*)model

{

    

    NSManagedObjectContext *cxt = [self managedObjectContext];

    

    NSEntityDescription *entityDescription = [NSEntityDescription

                                              entityForName:@"Persistence04Note" inManagedObjectContext:cxt];

    

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:entityDescription];

    

    NSPredicate *predicate = [NSPredicate predicateWithFormat:

                              @"date =  %@", model.date];

    [request setPredicate:predicate];

    

    NSError *error = nil;

    NSArray *listData = [cxt executeFetchRequest:request error:&error];

    if ([listData count] > 0) {

        NoteManagedObject *note = [listData lastObject];

        [self.managedObjectContext deleteObject:note];

        

        NSError *savingError = nil;

        if ([self.managedObjectContext save:&savingError]){

            NSLog(@" ");

        } else {

            NSLog(@" ");

            return -1;

        }

    }

    

    return 0;

}



// Note 

-(int) modify:(Persistence04Note*)model

{

    NSManagedObjectContext *cxt = [self managedObjectContext];

    

    NSEntityDescription *entityDescription = [NSEntityDescription

                                              entityForName:@"Persistence04Note" inManagedObjectContext:cxt];

    

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:entityDescription];

    

    NSPredicate *predicate = [NSPredicate predicateWithFormat:

                              @"date =  %@", model.date];

    [request setPredicate:predicate];

    

    NSError *error = nil;

    NSArray *listData = [cxt executeFetchRequest:request error:&error];

    if ([listData count] > 0) {

        NoteManagedObject *note = [listData lastObject];

        note.content = model.content;

        

        NSError *savingError = nil;

        if ([self.managedObjectContext save:&savingError]){

            NSLog(@" ");

        } else {

            NSLog(@" ");

            return -1;

        }

    }

    return 0;

}



// 

-(NSMutableArray*) findAll

{

    NSManagedObjectContext *cxt = [self managedObjectContext];

    

    NSEntityDescription *entityDescription = [NSEntityDescription

                                              entityForName:@"Persistence04Note" inManagedObjectContext:cxt];

    

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:entityDescription];

    

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];

    [request setSortDescriptors:@[sortDescriptor]];

    

    NSError *error = nil;

    NSArray *listData = [cxt executeFetchRequest:request error:&error];

    

    NSMutableArray *resListData = [[NSMutableArray alloc] init];

    

    for (NoteManagedObject *mo in listData) {

        Persistence04Note *note = [[Persistence04Note alloc] init];

        note.date = mo.date;

        note.content = mo.content;

        [resListData addObject:note];

    }

    

    return resListData;

}



// 

-(Persistence04Note*) findById:(Persistence04Note*)model

{

    NSManagedObjectContext *cxt = [self managedObjectContext];

    

    NSEntityDescription *entityDescription = [NSEntityDescription

                                              entityForName:@"Persistence04Note" inManagedObjectContext:cxt];

    

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

    [request setEntity:entityDescription];

    

    NSPredicate *predicate = [NSPredicate predicateWithFormat:

                              @"date =  %@",model.date];

    [request setPredicate:predicate];

    

    NSError *error = nil;

    NSArray *listData = [cxt executeFetchRequest:request error:&error];

    

    if ([listData count] > 0) {

        NoteManagedObject *mo = [listData lastObject];

        

        Persistence04Note *note = [[Persistence04Note alloc] init];

        note.date = mo.date;

        note.content = mo.content;

        

        return note;

    }

    return nil;

}







@end

좋은 웹페이지 즐겨찾기