FMDatabaseQueue

本文介绍了一个使用FMDatabaseQueue进行数据库操作的并发测试案例。通过创建两个并发队列,分别插入10万条记录到数据库中,并记录了操作的时间消耗及执行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (void)addFMDatabaseQueue {
  FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:self.dbPath];
//    dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
//    dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL);
    dispatch_queue_t q1 = dispatch_queue_create("queue1", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t q2 = dispatch_queue_create("queue2", DISPATCH_QUEUE_CONCURRENT);
    // 10万条6分钟6秒 插入
    // 20万条查询不到2分钟
    dispatch_async(q1, ^{
        for (int i = 0; i < 100000; ++i) {
            [queue inDatabase:^(FMDatabase *db) {
                NSString * sql = @"insert into user (name, password) values(?, ?) ";
                NSString * name = [NSString stringWithFormat:@"queue111 %d", i];
                BOOL res = [db executeUpdate:sql, name, @"boy"];
                if (!res) {
                    debugLog(@"error1 to add db data: %@", name);
                } else {
                    debugLog(@"succ1 to add db data: %@", name);
                }
            }];
        }
    });

    dispatch_async(q2, ^{
        for (int i = 0; i < 100000; ++i) {
            [queue inDatabase:^(FMDatabase *db) {
                NSString * sql = @"insert into user (name, password) values(?, ?) ";
                NSString * name = [NSString stringWithFormat:@"queue222 %d", i];
                BOOL res = [db executeUpdate:sql, name, @"boy"];
                if (!res) {
                    debugLog(@"error2 to add db data: %@", name);
                } else {
                    debugLog(@"succ2 to add db data: %@", name);
                }
            }];
        }
    });

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值