對(duì)于數(shù)據(jù)操作澈缺,最重要的一點(diǎn)就是數(shù)據(jù)安全的問(wèn)題,在多線(xiàn)程中炕婶,線(xiàn)程安全是數(shù)據(jù)安全的首要前提姐赡,下面談?wù)凢MDB 是如何對(duì)多線(xiàn)程進(jìn)行處理的。
FMDB 單例中處理多線(xiàn)程
我們都知道FMDB 一個(gè)簡(jiǎn)單的使用就是調(diào)用它的單例模式
FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
先創(chuàng)建個(gè)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]);
}
插入數(shù)據(jù)的方法
- (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;
}
多個(gè)線(xiàn)程同時(shí)使用db會(huì)發(fā)生什么呢柠掂?
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]);
// 查詢(xún)
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];
});
結(jié)果
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
可以看到项滑,三條插入語(yǔ)句分別開(kāi)啟了三個(gè)線(xiàn)程2319873
、2319749
涯贞、2319758
枪狂,只有Michael
線(xiàn)程2319758
插入成功了危喉,另外兩條都提示了
1. The FMDatabase <FMDatabase: 0x7f8232c21f10> is currently in use.
2. error = not an error
查看一下Person 表中的數(shù)據(jù)
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);
}
結(jié)果,確實(shí)只有Michael
一條數(shù)據(jù)
2016-05-12 22:14:25.445 UCFMDBText[21523:1631615] identifier=2, name=Michael, age=42
所以州疾,多線(xiàn)程時(shí)在FMDB 單例中操作同一張表是存在風(fēng)險(xiǎn)的辜限,可能會(huì)造成數(shù)據(jù)丟失。
FMDatabaseQueue
FMDatabaseQueue 就是FMDB 為線(xiàn)程安全提供的解決方案严蓖。
接著上面的例子薄嫡,使用FMDatabaseQueue 代替單例再插入三條數(shù)據(jù)
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, ^{
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]);
// 查詢(xún)
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];
});
結(jié)果
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
通過(guò)上面的運(yùn)行結(jié)果可以看出三條insert 語(yǔ)句全部執(zhí)行成功,并且分別在三個(gè)線(xiàn)程2307639
颗胡、2307634
毫深、2307624
中,相互之間并沒(méi)有影響毒姨,可見(jiàn)FMDatabaseQueue 可以有效的保證其中執(zhí)行sql 的線(xiàn)程安全哑蔫。