zoukankan      html  css  js  c++  java
  • iOS.数据持久化.PersistenceLayer.对象归档

    #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
  • 相关阅读:
    bzoj 4012: [HNOI2015]开店
    POJ 1054 The Troublesome Frog
    POJ 3171 Cleaning Shifts
    POJ 3411 Paid Roads
    POJ 3045 Cow Acrobats
    POJ 1742 Coins
    POJ 3181 Dollar Dayz
    POJ 3040 Allowance
    POJ 3666 Making the Grade
    洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
  • 原文地址:https://www.cnblogs.com/cqchen/p/3793853.html
Copyright © 2011-2022 走看看