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:#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]; }]; }
相关文章推荐
- 使用java生成json数据
- iOS-Core-Animation-Advanced-Techniques(二)
- Python学习笔记一:数据类型与基本操作
- VS2005中SetUnhandledExceptionFilter函数应用
- svn胜过git的地方
- 报表示例——用Jasper report实现MongoDB join
- Android学习_17_SQLite数据库存储方式
- [JQ权威指南]第六天:使用attr获取与设置元素的属性
- 菜鸟的mongoDB学习---(三)MongoDB 的增、删、改、查(insert、remove、update、find)
- iOS越狱后必装软件
- 第二阶段团队进展报告(2)
- Qt Creater调试时一直出现:“DEBUGGER: Waiting for debug socket connect” 和“DEBUGGER: go to sleep”
- 实用USB术语详解
- 如何正确的使用 Android 中的 themes 和 styles
- 黑马程序员--Java基础知识总结
- 要么忍,要么滚(我现在选择忍)
- tachyon 实例
- 获取路径
- 第二阶段团队进展报告(1)
- 4-python学习——数据操作