1,FMDB 单例中处理多线程
我们都知道FMDB 一个简单的使用就是调用它的单例模式
FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
先创建个Person 表
NSString *sql = @"create table person (identifier integer primary key autoincrement, name text, age integer);";
BOOL success = [db executeStatements:sql];
if (!success) {
NSLog(@"error = %@", [db lastErrorMessage]);
}
插入数据的方法
- (BOOL)insertIntoTablePerson:(FMDatabase *)db arguments:(NSDictionary *)arguments {
BOOL success = [db executeUpdate:@"INSERT INTO person (identifier, name, age) VALUES (:identifier, :name, :age)"
withParameterDictionary:arguments];
if (!success) {
NSLog(@"error = %@", [db lastErrorMessage]);
}
return success;
}
多个线程同时使用db会发生什么呢?
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_async(group, queue, ^{
NSInteger identifier = 0;
NSString *name = @"Demon";
NSInteger age = 20;
NSDictionary *arguments = @{@"identifier": @(identifier),
@"name": name,
@"age": @(age)};
if ([self insertIntoTablePerson:db arguments:arguments]) {
NSLog(@"Demon 插入成功 - %@", [NSThread currentThread]);
}
});
dispatch_group_async(group, queue, ^{
NSInteger identifier = 1;
NSString *name = @"Jemmy";
NSInteger age = 25;
NSDictionary *arguments = @{@"identifier": @(identifier),
@"name": name,
@"age": @(age)};
if ([self insertIntoTablePerson:db arguments:arguments]) {
NSLog(@"Jemmy 插入成功 - %@", [NSThread currentThread]);
}
});
dispatch_group_async(group, queue, ^{
NSInteger identifier = 2;
NSString *name = @"Michael";
NSInteger age = 42;
NSDictionary *arguments = @{@"identifier": @(identifier),
@"name": name,
@"age": @(age)};
if ([self insertIntoTablePerson:db arguments:arguments]) {
NSLog(@"Michael 插入成功 - %@", [NSThread currentThread]);
}
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"完成 - %@", [NSThread currentThread]);
// 查询
FMResultSet *s = [db executeQuery:@"SELECT * FROM person"];
while ([s next]) {
int identifier = [s intForColumnIndex:0];
NSString *name = [s stringForColumnIndex:1];
int age = [s intForColumnIndex:2];
NSLog(@"identifier=%d, name=%@, age=%d", identifier, name, age);
}
[db close];
});
结果
2016-05-13 09:36:05.629 UCFMDBText[24362:2319873] The FMDatabase <FMDatabase: 0x7f8232c21f10> is currently in use.
2016-05-13 09:36:05.629 UCFMDBText[24362:2319749] The FMDatabase <FMDatabase: 0x7f8232c21f10> is currently in use.
2016-05-13 09:36:05.630 UCFMDBText[24362:2319873] error = not an error
2016-05-13 09:36:05.630 UCFMDBText[24362:2319749] error = not an error
2016-05-13 09:36:05.630 UCFMDBText[24362:2319758] Jemmy 插入成功 - <NSThread: 0x7f8232d07940>{number = 2, name = (null)}
2016-05-13 09:36:05.633 UCFMDBText[24362:2319700] 完成 - <NSThread: 0x7f8232c081e0>{number = 1, name = main}
2016-05-13 09:36:05.633 UCFMDBText[24362:2319700] identifier=1, name=Jemmy, age=25
可以看到,三条插入语句分别开启了三个线程2319873、2319749、2319758,只有Michael 线程2319758 插入成功了,另外两条都提示了
1. The FMDatabase <FMDatabase: 0x7f8232c21f10> is currently in use.
2. error = not an error
查看一下Person 表中的数据
FMResultSet *s = [db executeQuery:@"SELECT * FROM person"];
while ([s next]) {
int identifier = [s intForColumnIndex:0];
NSString *name = [s stringForColumnIndex:1];
int age = [s intForColumnIndex:2];
NSLog(@"identifier=%d, name=%@, age=%d", identifier, name, age);
}
结果,确实只有Michael 一条数据
所以,多线程时在FMDB 单例中操作同一张表是存在风险的,可能会造成数据丢失。
FMDatabaseQueue
FMDatabaseQueue 就是FMDB 为线程安全提供的解决方案。
接着上面的例子,使用FMDatabaseQueue 代替单例再插入三条数据
ispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_async(group, queue, ^{
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:@"/tmp/tmp.db"];
[queue inDatabase:^(FMDatabase *db) {
if ([db executeUpdate:@"INSERT INTO person VALUES (?, ?, ?)", @0, @"Demon", @20]) {
NSLog(@"Demon 插入成功 - %@", [NSThread currentThread]);
}
}];
});
dispatch_group_async(group, queue, ^{
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:@"/tmp/tmp.db"];
[queue inDatabase:^(FMDatabase *db) {
if ([db executeUpdate:@"INSERT INTO person VALUES (?, ?, ?)", @1, @"Jemmy", @25]) {
NSLog(@"Jemmy 插入成功 - %@", [NSThread currentThread]);
}
}];
});
dispatch_group_async(group, queue, ^{
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:@"/tmp/tmp.db"];
[queue inDatabase:^(FMDatabase *db) {
if ([db executeUpdate:@"INSERT INTO person VALUES (?, ?, ?)", @2, @"Michael", @42]) {
NSLog(@"Michael 插入成功 - %@", [NSThread currentThread]);
}
}];
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"完成 - %@", [NSThread currentThread]);
// 查询
FMResultSet *s = [db executeQuery:@"SELECT * FROM person"];
while ([s next]) {
int identifier = [s intForColumnIndex:0];
NSString *name = [s stringForColumnIndex:1];
int age = [s intForColumnIndex:2];
NSLog(@"identifier=%d, name=%@, age=%d", identifier, name, age);
}
[db close];
});
结果
2016-05-13 09:19:42.149 UCFMDBText[24345:2307639] Michael 插入成功 - <NSThread: 0x7fb7dbd242d0>{number = 2, name = (null)}
2016-05-13 09:19:42.216 UCFMDBText[24345:2307634] Jemmy 插入成功 - <NSThread: 0x7fb7dd820600>{number = 3, name = (null)}
2016-05-13 09:19:42.301 UCFMDBText[24345:2307624] Demon 插入成功 - <NSThread: 0x7fb7dbc0b630>{number = 4, name = (null)}
2016-05-13 09:19:42.301 UCFMDBText[24345:2307577] 完成 - <NSThread: 0x7fb7dd803300>{number = 1, name = main}
2016-05-13 09:19:42.302 UCFMDBText[24345:2307577] identifier=0, name=Demon, age=20
2016-05-13 09:19:42.302 UCFMDBText[24345:2307577] identifier=1, name=Jemmy, age=25
2016-05-13 09:19:42.303 UCFMDBText[24345:2307577] identifier=2, name=Michael, age=42
通过上面的运行结果可以看出三条insert 语句全部执行成功,并且分别在三个线程2307639、2307634、2307624中,相互之间并没有影响,可见FMDatabaseQueue 可以有效的保证其中执行sql 的线程安全。