iOS小集

FMDB与多线程的研究

2018-01-18  本文已影响228人  其字德安

多线程安全测试


    NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"tmps.db"];
    
    FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
    
    [db open];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
        for (NSInteger i = 0; i < 10; i++) {
            
            [db executeUpdate:@"INSERT INTO t_PetInfo(petNo, age, name, weight, hoster) VALUES(?, ?, ?, ?, ?);", @(i), @(2), @"myPet0", @(15.0), @"derain"];
        }
        
    });
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        for (NSInteger i = 0; i < 10; i++) {
            
            [db executeUpdate:@"INSERT INTO t_PetInfo(petNo, age, name, weight, hoster) VALUES(?, ?, ?, ?, ?);", @(i), @(2), @"myPet1", @(15.0), @"derain"];
        }
 
    });
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        

        for (NSInteger i = 0; i < 10; i++) {
            
            [db executeUpdate:@"INSERT INTO t_PetInfo(petNo, age, name, weight, hoster) VALUES(?, ?, ?, ?, ?);", @(i), @(2), @"myPet2", @(15.0), @"derain"];
        }

    });
    
    // 打印
2017-08-28 19:37:20.547 ABDataBase[22333:3419905] The FMDatabase <FMDatabase: 0x60000008f910> is currently in use.
2017-08-28 19:37:20.547 ABDataBase[22333:3419908] The FMDatabase <FMDatabase: 0x60000008f910> is currently in use.
2017-08-28 19:37:20.547 ABDataBase[22333:3419905] The FMDatabase <FMDatabase: 0x60000008f910> is currently in use.
2017-08-28 19:37:20.547 ABDataBase[22333:3419908] The FMDatabase <FMDatabase: 0x60000008f910> is currently in use.
2017-08-28 19:37:20.548 ABDataBase[22333:3419905] The FMDatabase <FMDatabase: 0x60000008f910> is currently in use.
image.png
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
        for (NSInteger i = 0; i < 5; i++) {
            
            [self.queue inDatabase:^(FMDatabase *db) {
                
                [db executeUpdate:@"INSERT INTO t_MyTable(name, age, no, class_id) VALUES(?, ?, ?, ?);", [NSString stringWithFormat:@"derain1"], @(20 + arc4random_uniform(10)), @(i), @(arc4random_uniform(10))];
            }];
        }
        
    });
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        for (NSInteger i = 0; i < 5; i++) {
            
            [self.queue inDatabase:^(FMDatabase *db) {
                
                [db executeUpdate:@"INSERT INTO t_MyTable(name, age, no, class_id) VALUES(?, ?, ?, ?);", [NSString stringWithFormat:@"derain2"], @(20 + arc4random_uniform(10)), @(i), @(arc4random_uniform(10))];
            }];
        }
 
    });
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        for (NSInteger i = 0; i < 5; i++) {
            
            [self.queue inDatabase:^(FMDatabase *db) {
                
                [db executeUpdate:@"INSERT INTO t_MyTable(name, age, no, class_id) VALUES(?, ?, ?, ?);", [NSString stringWithFormat:@"derain3"], @(20 + arc4random_uniform(10)), @(i), @(arc4random_uniform(10))];
            }];
        }
    });
    
image.png
    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
             
            for (NSInteger i = 0; i < 5; i++) {
                
                [db executeUpdate:@"INSERT INTO t_MyTable(name, age, no, class_id) VALUES(?, ?, ?, ?);", [NSString stringWithFormat:@"derain0"], @(20 + arc4random_uniform(10)), @(i), @(arc4random_uniform(10))];
            }
                
        }];

    });
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
            
            for (NSInteger i = 0; i < 5; i++) {
                
                [db executeUpdate:@"INSERT INTO t_MyTable(name, age, no, class_id) VALUES(?, ?, ?, ?);", [NSString stringWithFormat:@"derain1"], @(20 + arc4random_uniform(10)), @(i), @(arc4random_uniform(10))];
            }
            
        }];
 
    });
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [self.queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
            
            for (NSInteger i = 0; i < 5; i++) {
                
                [db executeUpdate:@"INSERT INTO t_MyTable(name, age, no, class_id) VALUES(?, ?, ?, ?);", [NSString stringWithFormat:@"derain2"], @(20 + arc4random_uniform(10)), @(i), @(arc4random_uniform(10))];
            }
            
        }];
    });

- (void)inDatabase:(void (^)(FMDatabase *db))block {
    
    dispatch_sync(_queue, ^() {
        
        FMDatabase *db = [self database];
        
        block(db);
        
    FMDBRelease(self);
}
上一篇下一篇

猜你喜欢

热点阅读