前言
最近剛做完一個(gè)列表頁的數(shù)據(jù)緩存,我同時(shí)探討FMDB和CoreData兩種做法前痘,下面先說下FMDB的過程攀甚。
過程
FMDB框架導(dǎo)入比較簡(jiǎn)單,上github看下就好馁启。
之前也有用過FMDB存儲(chǔ)驾孔,那時(shí)要存的數(shù)據(jù)比較簡(jiǎn)單,都是普通的字符串?dāng)?shù)據(jù)惯疙,但是這次要存的數(shù)據(jù)比較復(fù)雜翠勉,有自定義對(duì)象,還有數(shù)組螟碎,數(shù)組內(nèi)的還是自定義對(duì)象眉菱。下面主要說下這種做法:
下面是我要存儲(chǔ)的people對(duì)象
@interface People : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *age;
@property(nonatomic,strong)NSArray *pets;//存Pet對(duì)象
@property(nonatomic,strong)Car *car;
@end
people對(duì)象里的Car對(duì)象
@interface Car : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *age;
@end
people對(duì)象中pets數(shù)組存儲(chǔ)的Pet對(duì)象
@interface Pet : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *age;
@end
以上就是我的數(shù)據(jù)結(jié)構(gòu)迹栓,下面說下怎么存儲(chǔ):
首先sqlite支持的存儲(chǔ)類型
NULL掉分,值是NULL
INTEGER俭缓,值是有符號(hào)整形,根據(jù)值的大小以1,2,3,4,6或8字節(jié)存放
REAL酥郭,值是浮點(diǎn)型值华坦,以8字節(jié)IEEE浮點(diǎn)數(shù)存放
TEXT,值是文本字符串不从,使用數(shù)據(jù)庫編碼(UTF-8惜姐,UTF-16BE或者UTF-16LE)存放
BLOB,二進(jìn)制數(shù)據(jù)(iOS的NSData)
一般存字符串都用text椿息,即使數(shù)字或者布爾值歹袁,也建議轉(zhuǎn)換成字符串統(tǒng)一存儲(chǔ)。
而存儲(chǔ)自定義的對(duì)象關(guān)鍵就是要轉(zhuǎn)換成NSData類型并已BLOB類型存儲(chǔ)寝优。
此處需要用到歸檔和解檔
#import "Car.h"
@interface Car ()<NSCoding>
@end
@implementation Car
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeObject:self.age forKey:@"age"];
}
- (nullable instancetype)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (self!=nil) {
self.name = [decoder decodeObjectForKey:@"name"];
self.age = [decoder decodeObjectForKey:@"age"];
}
return self;
}
@end
歸解檔參考這篇文章
自定義對(duì)象遵循NScoding協(xié)議条舔,并實(shí)現(xiàn)兩個(gè)方法:
-(void)encodeWithCoder:(NSCoder *)aCoder //以keyValue形式對(duì)基本數(shù)據(jù)類型Encoding
-(id)initWithCoder:(NSCoder *)aDecoder //以keyValue形式對(duì)基本數(shù)據(jù)類型Decoding,返回?cái)?shù)據(jù)模型本身
當(dāng)然如果覺得實(shí)現(xiàn)這個(gè)協(xié)議比較麻煩乏矾,可以把對(duì)象先轉(zhuǎn)化成字典孟抗,再直接調(diào)用NSKeyedArchiver的archivedDataWithRootObject方法轉(zhuǎn)換成data,因?yàn)樵跀?shù)組和字典是默認(rèn)遵循NScoding協(xié)議钻心,可以直接歸檔和解檔凄硼。
創(chuàng)建表單
-(void)qunueCreatPeopleTable{
NSString *creatTableString = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS People(people_name text, age text, pets blob, car blob)"];
[_queue inDatabase:^(FMDatabase *db) {
BOOL b = [db executeUpdate:creatTableString];
NSLog(@"create table is %d",b);
}];
}
插入數(shù)據(jù)
-(void)qunueInsertPeople:(People *)people{
//pets轉(zhuǎn)化data
NSData *petsData = [NSKeyedArchiver archivedDataWithRootObject:people.pets];
//car轉(zhuǎn)換data
NSData *carData = [NSKeyedArchiver archivedDataWithRootObject:people.car];
[_queue inDatabase:^(FMDatabase *db) {
BOOL insert = [db executeUpdate:@"INSERT INTO People (people_name, age, pets, car) VALUES (?,?,?,?)",people.name,people.age,petsData,carData];
if (insert) {
NSLog(@"添加成員成功!捷沸!");
}else{
NSLog(@"添加成員失斕痢!痒给!");
}
}];
}
獲取數(shù)據(jù)
-(NSArray *)qunueGetPeople{
__block NSMutableArray *dataArray = nil;
NSString *sql = [NSString stringWithFormat:@"SELECT *FROM People"];
[_queue inDatabase:^(FMDatabase *db) {
dataArray = [NSMutableArray array];
FMResultSet *result = [db executeQuery:sql];
while ([result next]) {
//從表單中獲取相應(yīng)字段的value
People *people = [People new];
people.name = [result stringForColumn:@"people_name"];
people.age = [result stringForColumn:@"age"];
//pets
NSData *petData = [result dataForColumn:@"pets"];
NSArray * arr = [NSKeyedUnarchiver unarchiveObjectWithData:petData];
//car
NSData *carData = [result dataForColumn:@"car"];
Car *car = [NSKeyedUnarchiver unarchiveObjectWithData:carData];
people.car = car;
people.pets = arr;
[dataArray addObject:people];
}
}];
return dataArray;
}
還有一點(diǎn)十分重要坯钦,自己在做這個(gè)功能時(shí),只要修改過表單結(jié)構(gòu)侈玄,都要?jiǎng)h掉原程序重新加載婉刀,不然會(huì)出現(xiàn)數(shù)據(jù)庫不能插入的情況。
最后給上我自己寫的demo
結(jié)束
學(xué)習(xí)之路序仙,與君共勉突颊。