FMDB 离线缓存

以新浪微博加载微博的工具类为例:

@implementation HWStatusTool

static FMDatabase* _db;

+ (void)initialize

{

//1.打开数据库

NSString*path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject] stringByAppendingPathComponent:@"statuses.sqlite"];

_db=  [FMDatabasedatabaseWithPath:path];//使用FMDB

[_dbopen];

//2.创表

[_db executeUpdate:@"CREATE TABLE IF NOT EXISTS t_status (id integer PRIMARY KEY, status blob NOT NULL, idstr text NOT NULL);"];

}

+ (NSArray*)statusesWithParams:(NSDictionary*)params

{

//根据请求参数生成对应的查询SQL语句

NSString*sql =nil;

if(params[@"since_id"]) {

sql = [NSString stringWithFormat:@"SELECT * FROM t_status WHERE idstr > %@ ORDER

BY idstr DESC LIMIT 20;", params[@"since_id"]];

}elseif(params[@"max_id"]) {

sql = [NSStringstringWithFormat:@"SELECT * FROM t_status WHERE idstr <= %@ ORDER

BY idstr DESC LIMIT 20;", params[@"max_id"]];

}else{

sql =@"SELECT * FROM t_status ORDER BY idstr DESC LIMIT

20;";

}

//执行SQL

FMResultSet*set = [_dbexecuteQuery:sql];

NSMutableArray*statuses = [NSMutableArray array];

while(set.next) {

NSData*statusData = [set objectForColumnName:@"status"];

NSDictionary*status = [NSKeyedUnarchiver unarchiveObjectWithData:statusData];//重点

[statuses addObject:status];

}

returnstatuses;

}

+ (void)saveStatuses:(NSArray*)statuses

{

//要将一个对象存进数据库的blob字段,最好先转为NSData

//一个对象要遵守NSCoding协议,实现协议中相应的方法,才能转成NSData

for(NSDictionary*status in statuses) {

// NSDictionary --> NSData

NSData*statusData = [NSKeyedArchiver archivedDataWithRootObject:status];//重点

[_db executeUpdateWithFormat:@"INSERT INTO t_status(status, idstr) VALUES (%@,

%@);", statusData, status[@"idstr"]];

}

}

@end

你可能感兴趣的:(FMDB 离线缓存)