您的位置:首页 > 其它

FMDB多线程

2015-06-23 10:27 232 查看
<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];
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  FMDB多线程