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
基于对象归档的实现
建立对象归档数据管理类
实现归档数据持久的对象实现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
相关文章推荐
- iOS hybrid App 的实现原理及性能监测
- OS开发指南读书笔记9(IOS数据持久层的建立1)
- IOS开发指南读书笔记8(AutoLayout) 2
- IOS开发指南读书笔记7(ScrollView)
- IOS开发指南读书笔记5(IOS 常用设计模式)
- IOS开发指南读书笔记4(IOS API分类)
- IOS开发指南读书笔记3(视图的生命周期)
- IOS开发指南读书笔记2(应用程序的生命周期)
- IOS开发指南读书笔记1(Xcode的基本使用)
- IOS滤镜处理
- iOS-openGLES程序制作基本步骤以及某些API
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS学习笔记44-IOS 遍历未知对象的属性和方法
- iOS调用系统相册、相机界面语言设置为中文
- iOS 支付宝开发流程
- 基于文档的应用程序
- 【iOS】自定义TabBarController
- iOS中的Masonry纯代码适配屏幕例子
- iOS 手机通讯录,将电话保存到本机通讯录
- 根据cell上的按钮找到当前的cell 比用btn.superview 要好 不会有ios7-8导致崩溃的问题