<span style="color:#cc0000;">之前在做電商的應用,裏面正好用到了數據庫,當時就看了一下FMDB的多線程,現在把項目中自己寫的一個多線程的Demo分享出來。</span>
<span style="color:#6600cc;">.h文件</span>
#import <Foundation/Foundation.h>
#import "FMDB.h"
@interface OKWDBQueueManager :NSObject
+(instancetype)defaultManager;
<span style="color:#009900;">/**
上游供應商列表</span>
<span style="color:#009900;"> 這裏實體Model自己定義,基本上這個包含了增刪查改,可自由替換
*/</span>
-(void)db_updateUpSupplierList:(OKWUpSupplierListModel *)upModel completedBlock:(void (^)(BOOL isSuccess))completedBlock;
-(OKWUpSupplierListModel *)upSupplierModelWithWeiNo:(NSString *)weiNo;
-(void)db_deleteAllDataUpSupplierModelWith:(NSString *)weiNo;
<span style="color:#6600cc;">.m文件</span>
#import "OKWDBQueueManager.h"
static NSString *const OKW_QUEUE_TABLE_UPSUPPLIER =@"OKW_QUEUE_TABLE_UPSUPPLIER";
static NSString *const OKW_QUEUE_TABLE_DOWNUSERS =@"OKW_QUEUE_TABLE_DOWNUSERS";
@interface OKWDBQueueManager()
@property(nonatomic,strong)FMDatabaseQueue * db;
@end
@implementation OKWDBQueueManager
#define DBQueuePath @"okw_queuedatasource.db"
static OKWDBQueueManager *sharedInstance =nil;
+(id)defaultManager
{
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[OKWDBQueueManageralloc] init];
sharedInstance.db = [FMDatabaseQueuedatabaseQueueWithPath:[sharedInstancegetDatabasePath]];
NSString * upSupplier_queue_table = [NSStringstringWithFormat:@"CREATE TABLE if not exists %@ (upID INTEGER PRIMARY KEY AUTOINCREMENT,WeiID,newMsgTitle,NewMsgCount)",OKW_QUEUE_TABLE_UPSUPPLIER];
NSString * downUsers_queue_table = [NSStringstringWithFormat:@"CREATE TABLE if not exists %@ (downID INTEGER PRIMARY KEY AUTOINCREMENT,WeiID,newMsgTitle,NewMsgCount)",OKW_QUEUE_TABLE_DOWNUSERS];
[sharedInstance.dbinDatabase:^(FMDatabase *db) {
if (![dbopen]) {
return;
}
[db executeUpdate:upSupplier_queue_table];
[db executeUpdate:downUsers_queue_table];
<span style="color:#009900;">// NSLog(@"%@表創建 %@",OKW_QUEUE_TABLE_UPSUPPLIER,([db executeUpdate:upSupplier_queue_table] ? @"成功" : @"失敗"));
// NSLog(@"%@表創建 %@",OKW_QUEUE_TABLE_DOWNUSERS,([db executeUpdate:downUsers_queue_table] ? @"成功" : @"失敗"));</span>
}];
});
returnsharedInstance;
}
-(NSString *)getDatabasePath
{
NSString *documentPath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0];
<span style="color:#009900;">// NSLog(@"DBQueuePath = %@",documentPath);</span>
return [documentPathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@",DBQueuePath]];
}
<span style="color:#009900;">/**
上游供應商列表
*/</span>
-(void)db_updateUpSupplierList:(OKWUpSupplierListModel *)upModel completedBlock:(void (^)(BOOL isSuccess))completedBlock{
[sharedInstance.dbinDatabase:^(FMDatabase *db) {
if (![dbopen]) {
return ;
}
NSString * resultSetString = [NSStringstringWithFormat:@"select * from OKW_QUEUE_TABLE_UPSUPPLIER where WeiID = '%@'",upModel.WeiID];
FMResultSet * resultSet = [dbexecuteQuery:resultSetString];
while ([resultSetnext]) {
BOOL res = [dbexecuteUpdate:@"UPDATE OKW_QUEUE_TABLE_UPSUPPLIER SET newMsgTitle=?,NewMsgCount=? WHERE WeiID=?",upModel.NewMsgTitle,[NSNumbernumberWithInt:([resultSet intForColumn:@"NewMsgCount"]+1)],upModel.WeiID];
if (!res) {
NSLog(@"error when update db table");
}else{
NSLog(@"success to update db table %@",upModel.WeiID);
}
[db close];
if(completedBlock)completedBlock(res);
return ;
}
BOOL res = [dbexecuteUpdate:@"insert into OKW_QUEUE_TABLE_UPSUPPLIER (WeiID,newMsgTitle,NewMsgCount) VALUES (?,?,?)",upModel.WeiID,upModel.NewMsgTitle,[NSNumbernumberWithInteger:(upModel.NewMsgCount==0?1:upModel.NewMsgCount)]];
[db close];
if(completedBlock)completedBlock(res);
}];
}
-(OKWUpSupplierListModel *)upSupplierModelWithWeiNo:(NSString *)weiNo{
__blockOKWUpSupplierListModel * upSupplierModel = [[OKWUpSupplierListModelalloc] init];
[sharedInstance.dbinDatabase:^(FMDatabase *db) {
[db open];
NSString * resultSetString = [NSStringstringWithFormat:@"select * from OKW_QUEUE_TABLE_UPSUPPLIER where WeiID = '%@'",weiNo];
FMResultSet * resultSet = [dbexecuteQuery:resultSetString];
while ([resultSetnext]) {
upSupplierModel.WeiID = [resultSetstringForColumn:@"WeiID"];
upSupplierModel.NewMsgTitle = [resultSetstringForColumn:@"newMsgTitle"];
upSupplierModel.NewMsgCount = [resultSetintForColumn:@"NewMsgCount"];
}
[db close];
}];
return upSupplierModel;
}
-(void)db_deleteAllDataUpSupplierModelWith:(NSString *)weiNo{
[sharedInstance.dbinDatabase:^(FMDatabase *db) {
[db open];
[db executeUpdate:@"DELETE from OKW_QUEUE_TABLE_UPSUPPLIER where WeiID=?",weiNo];
[db close];
}];
}