iOS.데이터 영구화.PersistenceLayer.객체 아카이빙

13344 단어 ios
#import <Foundation/Foundation.h>

#import "Persistence02Note.h"



#define FILE_NAME @"Persistence02NotesList.archive"

#define ARCHIVE_KEY @"Persistence02NotesList"



@interface Persistence02NoteDAO : NSObject



+ (Persistence02NoteDAO*)sharedManager;



- (NSString *)applicationDocumentsDirectoryFile;

- (void)createEditableCopyOfDatabaseIfNeeded;



// Persistence02Note 

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



// Persistence02Note 

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



// Persistence02Note 

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



// 

-(NSMutableArray*) findAll;



// 

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



@end

 
#import "Persistence02NoteDAO.h"



@implementation Persistence02NoteDAO





static Persistence02NoteDAO *sharedManager = nil;



+ (Persistence02NoteDAO*)sharedManager

{

    static dispatch_once_t once;

    dispatch_once(&once, ^{        

        

        sharedManager = [[self alloc] init];

        [sharedManager createEditableCopyOfDatabaseIfNeeded];               

    });

    return sharedManager;

}





- (void)createEditableCopyOfDatabaseIfNeeded {

    

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *writableDBPath = [self applicationDocumentsDirectoryFile];

    

    BOOL dbexits = [fileManager fileExistsAtPath:writableDBPath];

    if (!dbexits) {

        

        NSString *path = [self applicationDocumentsDirectoryFile];

        

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

        

        NSDate *date1 = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];

        Persistence02Note* Persistence02Note1 = [[Persistence02Note alloc] init];

        Persistence02Note1.date = date1;

        Persistence02Note1.content = @"Welcome to MyPersistence02Note.";

        

        NSDate *date2 = [dateFormatter dateFromString:@"2011-12-04 16:01:03"];

        Persistence02Note* Persistence02Note2 = [[Persistence02Note alloc] init];

        Persistence02Note2.date = date2;

        Persistence02Note2.content = @" MyPersistence02Note。";

        

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



        

        [array addObject:Persistence02Note1];

        [array addObject:Persistence02Note2];

        

        NSMutableData * theData = [NSMutableData data];

        NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]

                                      initForWritingWithMutableData:theData];

        [archiver encodeObject:array forKey:ARCHIVE_KEY];

        [archiver finishEncoding];

        

        [theData writeToFile:path atomically:YES];

    }

}



- (NSString *)applicationDocumentsDirectoryFile {

    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSString *path = [documentDirectory stringByAppendingPathComponent:FILE_NAME];

    return path;

}





// Persistence02Note 

-(int) create:(Persistence02Note*)model

{

    NSString *path = [self applicationDocumentsDirectoryFile];

    NSMutableArray *array = [self findAll];

    

    [array addObject:model];    

    

    NSMutableData * theData = [NSMutableData data];

    NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]

                                  initForWritingWithMutableData:theData];

    [archiver encodeObject:array forKey:ARCHIVE_KEY];

    [archiver finishEncoding];

    [theData writeToFile:path atomically:YES];

    

    return 0;

}



// Persistence02Note 

-(int) remove:(Persistence02Note*)model

{

    NSString *path = [self applicationDocumentsDirectoryFile];

    NSMutableArray *array = [self findAll];

    

    for (Persistence02Note* Persistence02Note in array) {

        

        // 

        if ([Persistence02Note.date isEqualToDate:model.date]){

            [array removeObject: Persistence02Note];

            

            NSMutableData * theData = [NSMutableData data];

            NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]

                                          initForWritingWithMutableData:theData];

            [archiver encodeObject:array forKey:ARCHIVE_KEY];

            [archiver finishEncoding];

            [theData writeToFile:path atomically:YES];

            

            break;

        }

    }

    

    return 0;

}



// Persistence02Note 

-(int) modify:(Persistence02Note*)model

{

    

    NSString *path = [self applicationDocumentsDirectoryFile];

    NSMutableArray *array = [self findAll];

    

    for (Persistence02Note* Persistence02Note in array) {

        

        // 

        if ([Persistence02Note.date isEqualToDate:model.date]){

            

            Persistence02Note.content = model.content;

            

            NSMutableData * theData = [NSMutableData data];

            NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]

                                          initForWritingWithMutableData:theData];            

            [archiver encodeObject:array forKey:ARCHIVE_KEY];

            [archiver finishEncoding];

            

            [theData writeToFile:path atomically:YES];

            

            break;

        }

    }

    return 0;

}



// 

-(NSMutableArray*) findAll

{

    NSString *path = [self applicationDocumentsDirectoryFile];

    

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

    NSData * theData =[NSData dataWithContentsOfFile:path];

    

    if([theData length] > 0) {

        NSKeyedUnarchiver * archiver = [[NSKeyedUnarchiver alloc]

                                        initForReadingWithData:theData];

        listData = [archiver decodeObjectForKey:ARCHIVE_KEY];

        [archiver finishDecoding];



    }

    return listData;

}



// 

-(Persistence02Note*) findById:(Persistence02Note*)model

{

    NSString *path = [self applicationDocumentsDirectoryFile];

    

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

    NSData * theData =[NSData dataWithContentsOfFile:path];

    

    if([theData length] > 0) {

        NSKeyedUnarchiver * archiver = [[NSKeyedUnarchiver alloc]

                                        initForReadingWithData:theData];

        listData = [archiver decodeObjectForKey:ARCHIVE_KEY];

        [archiver finishDecoding];

        

        for (Persistence02Note* Persistence02Note in listData) {

            

            // 

            if ([Persistence02Note.date isEqualToDate:model.date]){

                return Persistence02Note;

            }

        }

    }

    return nil;

}





@end

좋은 웹페이지 즐겨찾기