本文主要是介绍iOS下FMDB的多线程操作(二),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上一篇记录不使用FMDatabaseQueue来使用多线程,这一篇记录一下使用FMDatabaseQueue的方式。
需要注意的时queue操作中不能嵌套queue操作,否则会各种错误。
当使用FMDatabaseQueue,因为整个项目用同一个queue,所以最好写一个单例。我是这么写的,不知道是否合理,先记录吧。
下面是头文件。
@interface JKDataBase : NSObject@property (nonatomic, retain, readonly) FMDatabaseQueue *dbQueue;+ (JKDataBase *)shareInstance;
.m文件就贴一下关键代码。
static JKDataBase *_instance = nil;+ (instancetype)shareInstance
{static dispatch_once_t onceToken ;dispatch_once(&onceToken, ^{_instance = [[super allocWithZone:NULL] init] ;}) ;return _instance;
}- (FMDatabaseQueue *)dbQueue
{if (_dbQueue == nil) {NSString* docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject];NSFileManager *filemanage = [NSFileManager defaultManager];docsdir = [docsdir stringByAppendingPathComponent:@"FMDBDemo"];BOOL isDir;BOOL exit =[filemanage fileExistsAtPath:docsdir isDirectory:&isDir];if (!exit || !isDir) {[filemanage createDirectoryAtPath:docsdir withIntermediateDirectories:YES attributes:nil error:nil];}NSString* dbpath = [docsdir stringByAppendingPathComponent:@"myDB.sqlite"];_dbQueue = [[FMDatabaseQueue alloc] initWithPath:dbpath];}return _dbQueue;
}
然后就是User类中的数据库操作。同样也是创建表、插入、删除、更新、查询五类操作。
1、创建数据库表
/** 创建表 */
+ (BOOL)createUserTable
{__block BOOL res = NO;JKDataBase *jkDB = [JKDataBase shareInstance];[jkDB.dbQueue inDatabase:^(FMDatabase *db) {NSString *sql = @"CREATE TABLE IF NOT EXISTS User(ID INTEGER PRIMARY KEY, name TEXT, age INTEGER, ID_No TEXT);";res = [db executeUpdate:sql];}];return res;
}
2、插入数据
- (BOOL)save
{JKDataBase *jkDB = [JKDataBase shareInstance];__block BOOL res = NO;[jkDB.dbQueue inDatabase:^(FMDatabase *db) {NSString *sql = [NSString stringWithFormat:@"INSERT INTO User(name, age, ID_No) VALUES ('%@', '%d', '%@');", self.name, self.age, self.ID_no];res = [db executeUpdate:sql];}];return res;
}/** 批量保存用户对象 */
+ (BOOL)saveObjects:(NSArray *)array
{__block BOOL res = YES;JKDataBase *jkDB = [JKDataBase shareInstance];// 如果要支持事务[jkDB.dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {for (User *user in array) {NSString *sql = [NSString stringWithFormat:@"INSERT INTO User(name, age, ID_No) VALUES ('%@', '%d', '%@');", user.name, user.age, user.ID_no];if (![db executeUpdate:sql]) {res = NO;*rollback = YES;return;}}}];return res;
}
3、更新数据
- (BOOL)update
{JKDataBase *jkDB = [JKDataBase shareInstance];__block BOOL res = NO;[jkDB.dbQueue inDatabase:^(FMDatabase *db) {NSString *sql = [NSString stringWithFormat:@"UPDATE User SET name = '%@',age = '%d',ID_no = '%@' WHERE ID = '%d';", self.name, self.age, self.ID_no, self.ID];res = [db executeUpdate:sql];}];return res;
}/** 批量更新用户对象*/
+ (BOOL)updateObjects:(NSArray *)array
{__block BOOL res = YES;JKDataBase *jkDB = [JKDataBase shareInstance];// 如果要支持事务[jkDB.dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {for (User *user in array) {NSString *sql = [NSString stringWithFormat:@"UPDATE User SET name = '%@',age = '%d',ID_no = '%@' WHERE ID = '%d';", user.name, user.age, user.ID_no, user.ID];BOOL flag = [db executeUpdate:sql];if (!flag) {res = NO;*rollback = YES;return;}}}];return res;
}
4、删除数据
- (BOOL)deleteObject
{JKDataBase *jkDB = [JKDataBase shareInstance];__block BOOL res = NO;[jkDB.dbQueue inDatabase:^(FMDatabase *db) {NSString *sql = [NSString stringWithFormat:@"DELETE FROM User WHERE ID = '%d'",self.ID];res = [db executeUpdate:sql];}];return res;
}/** 批量删除用户对象 */
+ (BOOL)deleteObjects:(NSArray *)array
{__block BOOL res = YES;JKDataBase *jkDB = [JKDataBase shareInstance];// 如果要支持事务[jkDB.dbQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {for (User *user in array) {NSString *sql = [NSString stringWithFormat:@"DELETE FROM User WHERE ID = '%d'",user.ID];BOOL flag = [db executeUpdate:sql];if (!flag) {res = NO;*rollback = YES;return;}}}];return res;
}
5、查询数据
+ (NSArray *)findAll
{JKDataBase *jkDB = [JKDataBase shareInstance];NSMutableArray *users = [[NSMutableArray alloc] init];[jkDB.dbQueue inDatabase:^(FMDatabase *db) {NSString *sql = @"SELECT * FROM User";FMResultSet *resultSet = [db executeQuery:sql];while ([resultSet next]) {User *user = [[User alloc] init];user.ID = [resultSet intForColumn:@"ID"];user.name = [resultSet stringForColumn:@"name"];user.age = [resultSet intForColumn:@"age"];user.ID_no = [resultSet stringForColumn:@"ID_no"];[users addObject:user];[user release];}}];return [users autorelease];
}/** 查找某个用户 */
+ (instancetype)findFirstBySql:(NSString *)sql
{User *user = nil;NSArray *users = [self findBySql:sql];if (users.count > 0) {user = [users firstObject];}return user;
}/** 查找用户 */
+ (NSArray *)findBySql:(NSString *)sql
{JKDataBase *jkDB = [JKDataBase shareInstance];NSMutableArray *users = [[NSMutableArray alloc] init];[jkDB.dbQueue inDatabase:^(FMDatabase *db) {NSString *select = @"SELECT * FROM User ";if (sql) {select = [select stringByAppendingString:sql];}FMResultSet *resultSet = [db executeQuery:select];while ([resultSet next]) {User *user = [[User alloc] init];user.ID = [resultSet intForColumn:@"ID"];user.name = [resultSet stringForColumn:@"name"];user.age = [resultSet intForColumn:@"age"];user.ID_no = [resultSet stringForColumn:@"ID_no"];[users addObject:user];[user release];}}];return [users autorelease];
}
先记录这么多吧,希望能对别人有所帮助。
demo传送门:http://download.csdn.net/detail/u011619283/8838965
这篇关于iOS下FMDB的多线程操作(二)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!