使用 YTKKeyValueStore 做 iOS離線緩存(FMDB)

另一種

JQFMDB

FMDB的封裝,操作簡單,線程安全,擴展性強,直接操作model或dictionary


YTKKeyValueStore介紹

YTKKeyValueStore是iOS端的一個 Key-Value 存儲類庫。

封裝了fmdb,使用Key-Value式的存儲。在存儲量不大的情況下,開發上的效率優勢很大:

1.Model層的代碼編寫簡單,易于測試。

2.由于Value是JSON格式,所以在做Model字段更改時,易于擴展和兼容。

安裝

在 Podfile 中加入下面一行代碼來使用YTKKeyValueStore

pod ‘YTKKeyValueStore’

也可以手動添加源碼 YTKKeyValueStore.h和YTKKeyValueStore.m 到你的工程中,并且在工程設置的Link Binary With Libraries中,增加libsqlite3.dylib

使用

所有的接口都封裝在YTKKeyValueStore類中。以下是一些常用方法說明。

打開(或創建)數據庫

// 打開名為test.db的數據庫,如果該文件不存在,則創新一個新的。

YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"test.db"];

創建數據庫表

YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"test.db"];

NSString *tableName = @"user_table";

// 創建名為user_table的表,如果已存在,則忽略該操作

[store createTableWithName:tableName];

讀寫數據

YTKKeyValueStore類提供key-value的存儲接口,存入的所有數據需要提供key以及其對應的value,讀取的時候需要提供key來獲得相應的value。

YTKKeyValueStore類支持的value類型包括:NSString, NSNumber, NSDictionary和NSArray,為此提供了以下接口:

- (void)putString:(NSString *)string withId:(NSString *)stringId intoTable:(NSString *)tableName;

- (void)putNumber:(NSNumber *)number withId:(NSString *)numberId intoTable:(NSString *)tableName;

- (void)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)tableName;

與此對應,有以下value為NSString, NSNumber, NSDictionary和NSArray的讀取接口:

- (NSString *)getStringById:(NSString *)stringId fromTable:(NSString *)tableName;

- (NSNumber *)getNumberById:(NSString *)numberId fromTable:(NSString *)tableName;

- (id)getObjectById:(NSString *)objectId fromTable:(NSString *)tableName;

刪除數據接口

YTKKeyValueStore提供了以下接口用于刪除數據。

// 清除數據表中所有數據

- (void)clearTable:(NSString *)tableName;

// 刪除指定key的數據

- (void)deleteObjectById:(NSString *)objectId fromTable:(NSString *)tableName;

// 批量刪除一組key數組的數據

- (void)deleteObjectsByIdArray:(NSArray *)objectIdArray fromTable:(NSString *)tableName;

// 批量刪除所有帶指定前綴的數據

- (void)deleteObjectsByIdPrefix:(NSString *)objectIdPrefix fromTable:(NSString *)tableName;

更多接口

YTKKeyValueStore 還提供了以下接口來獲取表示內部存儲的key-value對象。

// 獲得指定key的數據

- (YTKKeyValueItem *)getYTKKeyValueItemById:(NSString *)objectId fromTable:(NSString *)tableName;

// 獲得所有數據

- (NSArray *)getAllItemsFromTable:(NSString *)tableName;

在項目中的實際使用

在薄荷中 BHGlobalStore.h 封裝了 YTKKeyValueStore

@property (nonatomic, strong) YTKKeyValueStore *store;

- (void)putGlobalObject:(id)object withId:(NSString *)objectId;

- (id)globalObjectById:(NSString *)objectId;

- (void)deleteGlobalObjectById:(NSString *)objectId;

- (void)clearGlobalObjects;

BHGlobalStore.m 實現

初始化就創建了一個數據庫 kDatabase 并且創建一個表 kGlobal,其它功能就是封裝對這張表的操作,用來保證app中只有這一個數據庫和表,方便管理。

- (instancetype)init

{

self = [super init];

if ( self ) {

self.store = [[YTKKeyValueStore alloc] initDBWithName:kDatabase];

[self.store createTableWithName:kGlobal];

}

return self;

}

- (void)putGlobalObject:(id)object withId:(NSString *)objectId

{

[self.store putObject:object withId:objectId intoTable:kGlobal];

}

- (id)globalObjectById:(NSString *)objectId

{

return [self.store getObjectById:objectId fromTable:kGlobal];

}

- (void)deleteGlobalObjectById:(NSString *)objectId

{

[self.store deleteObjectById:objectId fromTable:kGlobal];

}

- (void)clearGlobalObjects

{

[self.store clearTable:kGlobal];

}

薄荷中使用Mantle處理Model層對象

@interface BHActiveModel : MTLModel

BINGO_RESP_SPORTS_COURSES_COURSE_ID_SPORTS_DAYS 又繼承了 BHActiveModel

存到表中

-(void)saveToDB:(BINGO_RESP_SPORTS_COURSES_COURSE_ID_SPORTS_DAYS *)x{

//將model轉為json對象

NSDictionary *dic = [MTLJSONAdapter JSONDictionaryFromModel:x];

[app.store putGlobalObject:dic withId:PXSportsCourseRecordKey];

}

從表中取出

NSDictionary * dict = [app.store globalObjectById:PXSportsCourseForCourseId(self.courseId)];

if(dict){

//將json轉為model

BINGO_SPORTS_DAYS_DETAIL * cache =[BINGO_SPORTS_DAYS_DETAIL fromDictionary:dict];

總結

1.使用非常的方便 不用去管表的列 類型了,存取就一句代碼。

3.因為存的是json,就算model改變或擴展,只需要改model其它業務代碼都不需要更改。

4.適合數據量不大的存儲

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容