前言
最近刚做完一个列表页的数据缓存,我同时探讨FMDB和CoreData两种做法,下面先说下FMDB的过程。
过程
FMDB框架导入比较简单,上github看下就好。
之前也有用过FMDB存储,那时要存的数据比较简单,都是普通的字符串数据,但是这次要存的数据比较复杂,有自定义对象,还有数组,数组内的还是自定义对象。下面主要说下这种做法:
下面是我要存储的people对象
@interface People : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *age;
@property(nonatomic,strong)NSArray *pets;//存Pet对象
@property(nonatomic,strong)Car *car;
@end
people对象里的Car对象
@interface Car : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *age;
@end
people对象中pets数组存储的Pet对象
@interface Pet : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *age;
@end
以上就是我的数据结构,下面说下怎么存储:
首先sqlite支持的存储类型
NULL,值是NULL
INTEGER,值是有符号整形,根据值的大小以1,2,3,4,6或8字节存放
REAL,值是浮点型值,以8字节IEEE浮点数存放
TEXT,值是文本字符串,使用数据库编码(UTF-8,UTF-16BE或者UTF-16LE)存放
BLOB,二进制数据(iOS的NSData)
一般存字符串都用text,即使数字或者布尔值,也建议转换成字符串统一存储。
而存储自定义的对象关键就是要转换成NSData类型并已BLOB类型存储。
此处需要用到归档和解档
#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
归解档参考这篇文章
自定义对象遵循NScoding协议,并实现两个方法:
-(void)encodeWithCoder:(NSCoder *)aCoder //以keyValue形式对基本数据类型Encoding
-(id)initWithCoder:(NSCoder *)aDecoder //以keyValue形式对基本数据类型Decoding,返回数据模型本身
当然如果觉得实现这个协议比较麻烦,可以把对象先转化成字典,再直接调用NSKeyedArchiver的archivedDataWithRootObject方法转换成data,因为在数组和字典是默认遵循NScoding协议,可以直接归档和解档。
创建表单
-(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);
}];
}
插入数据
-(void)qunueInsertPeople:(People *)people{
//pets转化data
NSData *petsData = [NSKeyedArchiver archivedDataWithRootObject:people.pets];
//car转换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(@"添加成员失败!!");
}
}];
}
获取数据
-(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]) {
//从表单中获取相应字段的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;
}
还有一点十分重要,自己在做这个功能时,只要修改过表单结构,都要删掉原程序重新加载,不然会出现数据库不能插入的情况。
最后给上我自己写的demo
结束
学习之路,与君共勉。