FMDB常用API簡述及多線程單例類封裝

FMDB的簡單使用

  • 導(dǎo)入頭文件 #import "FMDB.h"
  • 全局的靜態(tài)數(shù)據(jù)庫對象
    initialize
    // 這個類在第一次使用時就被調(diào)用一次,后面就不會再調(diào)用;而且是先于實例化方法調(diào)用的.
static FMDatabase *_db;
+ (void)initialize {
 // NSLog(@"這個類在第一次使用時就被調(diào)用一次"); 
// 1.獲取數(shù)據(jù)庫路徑
 NSString *SQLPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"heros.db"];//heros是你的表名
 // 2.創(chuàng)建數(shù)據(jù)庫和建表,只會執(zhí)行一次 
_db = [FMDatabase databaseWithPath:SQLPath];
 // 3.打開數(shù)據(jù)庫
 BOOL isOpen = [_db open]; 
if (isOpen) { 
  // 4.建表 
  BOOL isCreate = [_db executeUpdate:@"create table if not exists t_heros(id integer primary key,name text not null,age integer)"];
   if (isCreate) {
     NSLog(@"建表成功");
    } 
  } 
}

新增

[_db executeUpdateWithFormat:@"insert into t_heros(name,age) values('張三',18)"];

修改

[_db executeUpdateWithFormat:@"update t_heros set age = 19 where name = '張三'"];

刪除

[_db executeUpdateWithFormat:@"delete from t_heros where name = '張三'"];

查詢 -- 模糊查詢

// 執(zhí)行查詢語句.獲取到結(jié)果集
FMResultSet *resultSet = [_db executeQuery:@"select * from t_heros"];
// 遍歷結(jié)果集,取數(shù)據(jù)
while ([resultSet next]) {
 // 取出來的數(shù)據(jù) 
NSString *name = [resultSet stringForColumn:@"name"];
 int age = [resultSet intForColumn:@"age"];
}

如果數(shù)據(jù)庫里面的記錄有很多條,那么就要像解析模型一樣創(chuàng)建數(shù)據(jù)源去接收多條數(shù)據(jù)存到數(shù)據(jù)源

+ (NSArray *)selectHeros{  
  // 定義臨時的模型數(shù)據(jù)  
  NSMutableArray *tmpM = [NSMutableArray array];  
  // 執(zhí)行查詢語句.獲取到結(jié)果集  
  FMResultSet *resultSet = [_db executeQuery:@"select * from t_heros"];  
  // 遍歷結(jié)果集,取數(shù)據(jù)  
  while ([resultSet next]) {       
 // 創(chuàng)建模型       
 Heros *hero = [[Heros alloc] init];     //自己的模型 
  // 取出來的數(shù)據(jù)    
    NSString *name = [resultSet stringForColumn:@"name"];     
   int age = [resultSet intForColumn:@"age"];  
      // 給模型賦值    ,name和age為模型里面的key,根據(jù)自己的實際情況更改
    hero.name = name;        
    hero.age = @(age);     
   // 將模型添加到模型數(shù)組      
  [tmpM addObject:hero];  
  }   
 return tmpM.copy;

到此簡單的數(shù)據(jù)庫增刪改查都可以實現(xiàn),那么問題來了

dispatch_async(dispatch_get_global_queue(0, 0), ^{
 [_db executeUpdateWithFormat:@"insert into t_heros(name,age) values('李四',19)"];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
[_db executeUpdateWithFormat:@"insert into t_heros(name,age) values('周五',20)"];
});

項目中不可能只是單線程的情況,那么多線程情況下就有可能出現(xiàn)多條數(shù)據(jù)插入,只有一條數(shù)據(jù)成功的情況,因為FMDB默認(rèn)不支持多線程去操作數(shù)據(jù)庫,

分析FMDatabaseQueue
源碼

- (void)inDatabase:(void (^)(FMDatabase *db))block { 
/* Get the currently executing queue (which should probably be nil, but in theory could be another DB queue * and then check it against self to make sure we're not about to deadlock. */ 
FMDatabaseQueue *currentSyncQueue = (__bridge id)dispatch_get_specific(kDispatchQueueSpecificKey); 
assert(currentSyncQueue != self && "inDatabase: was called reentrantly on the same queue, which would lead to a deadlock");
 FMDBRetain(self); 
// 串行隊列+同步任務(wù)
 dispatch_sync(_queue, ^() {
 // 數(shù)據(jù)庫實例
 FMDatabase *db = [self database];
 block(db); 
if ([db hasOpenResultSets]) {
 NSLog(@"Warning: there is at least one open result set around after performing [FMDatabaseQueue inDatabase:]");
#if defined(DEBUG) && DEBUG 
NSSet *openSetCopy = FMDBReturnAutoreleased(
[[db valueForKey:@"_openResultSets"] copy]);
 for (NSValue *rsInWrappedInATastyValueMeal in openSetCopy) {
 FMResultSet *rs = (FMResultSet *)[rsInWrappedInATastyValueMeal pointerValue]; 
NSLog(@"query: '%@'", [rs query]);
 }
#endif 
} 
});
 FMDBRelease(self);}

結(jié)論 :
FMDatabaseQueue管理著一個隊里,這個隊列是串行隊列.
串行隊列里面裝的都是同步任務(wù)
同步任務(wù)里面就是操作數(shù)據(jù)庫的代碼
所以如果我們的 app 需要多線程操作數(shù)據(jù)庫,那么就需要使用 FMDatabaseQueue 來保證線程安全了。切記不能在多個線程中共同一個 FMDatabase 對象并且在多個線程中同時使用,這個類本身不是線程安全的,這樣使用會造成數(shù)據(jù)混亂等問題,我么需要實例化一個 FMDatabaseQueue區(qū)吃力多線程下的數(shù)據(jù)庫操作

封裝FMDatabaseQueue單例類
把FMDatabaseQueue定義成單例的目的是為了保證隊列在內(nèi)存中是唯一的一個,當(dāng)有多個數(shù)據(jù)庫操作任務(wù)時,都可以放在同一個隊列中.

單例類繼承自FMDatabaseQueue

#import "FMDB.h"
//繼承FMDatabaseQueue
@interface FMDatabaseQueueManager : FMDatabaseQueue
+ (instancetype)sharedDatabaseQueue;
@end

FMDatabaseQueue
單例類實現(xiàn)

+ (instancetype)sharedDatabaseQueueManager{
 static FMDatabaseQueueManager *instance; 
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
// 初始化隊列的操作
 instance = [FMDatabaseQueueManager databaseQueueWithPath:@"/xxx/xxx/xxx/heros.db"];//數(shù)據(jù)庫路徑
 }); 
return instance;
 }

//實例化單利之后再進(jìn)行數(shù)據(jù)庫的操作

//創(chuàng)建數(shù)據(jù)庫表

+ (void)initialize{
 // 創(chuàng)建和打開數(shù)據(jù)庫
 [[FMDatabaseQueueManager sharedDatabaseQueue] inDatabase:^(FMDatabase *db) {
 // 建表
 BOOL isCreate = [db executeUpdate:@"create table if not exists t_heros(id integer primary key,name text not null,age integer)"];
 if (isCreate) { 
    NSLog(@"建表成功");
 }
 }];
}
//以新增為例
[[FMDatabaseQueueManager sharedDatabaseQueue] inDatabase:^(FMDatabase *db) { 
       [db executeUpdateWithFormat:@"insert into t_heros(name,age) values('張三',28)"];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,820評論 25 709
  • 文/白茶心 自責(zé)沒有把新員工用好帶好 李青早早坐在咨詢室里等著圓圓。圓圓到了,坐下喝了口水,沒有說話。她看著墻上的...
    白茶心閱讀 1,094評論 5 3
  • 我心里有一張清晰又模糊的臉,他笑顏懵懂他沉默如斯他眉目清明。那些畫面一點一點從記憶里劃過,好似就是在昨天。 ...
    亦若顏閱讀 496評論 0 6
  • 昨天是個應(yīng)該被記住的日子 六點多下課我去買了人生第一包紅雙喜 揣著火機(jī)在黑漆漆靜悄悄的西操場 抽了人生第一根和第二...
    怪力雪人閱讀 563評論 0 0

友情鏈接更多精彩內(nèi)容