您的位置:首页 > 移动开发 > IOS开发

IOS开发指南读书笔记10(IOS数据持久层的建立2)

2015-11-18 11:40 459 查看
IOS开发指南读书笔记10(IOS数据持久层的建立2)

基于对象归档的实现

建立对象归档数据管理类

实现归档数据持久的对象实现NSCoding协议

其属性也必须是基本类型或者实现NSCoding协议的对象

我们堆Note做如下更改添加这两个方法

-(void)encodeWithCoder:(NSCoder *)aCoder

{

//编码

[aCoder encodeObject:[NSNumber numberWithInteger:_ID] forKey:@"ID"];

[aCoder encodeObject:_content forKey:@"content"];

}

-(id)initWithCoder:(NSCoder *)aDecoder

{

self = [super init];

if (self) {

//解码

self.ID = [aDecoder decodeIntegerForKey:@"ID"];

self.content = [aDecoder decodeObjectForKey:@"content"];

}

return self;

}

//归档数据管理

@interface ArchiveNoteDao : NSObject<BaseNoteDao>

#define ARCHIVE_KEY @"Notes"

@ end

具体实现

@implementation ArchiveNoteDao

+(id)sharedManager

{

static dispatch_once_t onceToken;

__block ArchiveNoteDao* instance = nil;

dispatch_once(&onceToken, ^{

instance = [[ArchiveNoteDao alloc]init];

[instance createObjectCacheFile];

});

return instance;

}

-(void)createObjectCacheFile

{

NSFileManager* fileManager = [NSFileManager defaultManager];

NSString* objectDataFilePath = [self ObjectCacheFilePath];

BOOL exist = [fileManager fileExistsAtPath:objectDataFilePath];

if (!exist) {

//添加一条伪数据

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

note.ID = 0;

note.content = @"今天天气晴转阴";

NSMutableArray* array = [@[note] mutableCopy];

//保存数组

[self saveWtihArray:array];

}

}

//根据数组保存到归档文件

-(void)saveWtihArray:(NSMutableArray*)array

{

NSString* objectDataFilePath = [self ObjectCacheFilePath];

NSMutableData* theData = [NSMutableData data];

NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:theData];

[archiver encodeObject:array forKey:ARCHIVE_KEY];

[archiver finishEncoding];

[theData writeToFile:objectDataFilePath atomically:YES];

}

-(NSString *)ObjectCacheFilePath

{

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

return [documentDirectory stringByAppendingPathComponent:@"Notes.archive"];

}

-(void)addNote:(Note *)note

{

NSMutableArray* datas = [self queryAllNote];

[datas addObject:note];

[self saveWtihArray:datas];

}

-(void)removeNote:(Note *)note

{

NSMutableArray* datas = [self queryAllNote];

for (Note* sNote in datas) {

if (sNote.ID == note.ID) {

[datas removeObject:sNote];

[self saveWtihArray:datas];

break;

}

}

}

-(void)modify:(Note *)note

{

NSMutableArray* datas = [self queryAllNote];

for (Note* sNote in datas) {

if (sNote.ID == note.ID) {

[sNote modifyWithOther:note];

[self saveWtihArray:datas];

break;

}

}

}

-(NSMutableArray*)queryAllNote

{

NSString* objectDataFilePath = [self ObjectCacheFilePath];

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

NSData* archiveData = [NSData dataWithContentsOfFile:objectDataFilePath];

if ([archiveData length] > 0) {

//反归档

NSKeyedUnarchiver* unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:archiveData];

datas = [unArchiver decodeObjectForKey:ARCHIVE_KEY];

[unArchiver finishDecoding];

}

return datas;

}

-(Note*)queryNoteByID:(NSInteger)ID

{

NSMutableArray* datas = [self queryAllNote];

for (Note* sNote in datas) {

if (sNote.ID == ID) {

return sNote;

}

}

return nil;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: