美文网首页收藏ios
FMDB性能优化-使用事务提升性能

FMDB性能优化-使用事务提升性能

作者: qiushuitian | 来源:发表于2017-08-08 16:27 被阅读156次

使用FMDB事务批量更新数据,速度会有大幅度提升。

下面的例子中可以看到,不使用事务,更新374条数据就用了6秒多;而使用事务,仅用了300多毫秒。(iPhone6测试)

不使用事务

FMDatabaseQueue * dbQueue = [SGVFoundationDataCenter shareInstance].dbQueue;    
[dbQueue inDatabase:^(FMDatabase *db) {
    // 清除数据
    NSLog(@"-- add Station begin count = %ld",stations.count);
    [db executeUpdate:@"delete from sgv_station where city_code = ?",@(cityCode)];
    [stations enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SGVSubwayStation * station = obj;
        [db executeUpdate:@"INSERT INTO sgv_station (city_code, line_code, station_code, station_name, line_inner_code, is_wifi_valid) VALUES (?,?,?,?,?,?)",@(cityCode), @(station.lineCode),@(station.stationCode), station.stationName, @(station.lineInnerCode),@(station.isWiFiValid)];
    }];
    NSLog(@"-- add Station end"];
}];

使用事务

FMDatabaseQueue * dbQueue = [SGVFoundationDataCenter shareInstance].dbQueue;    
[dbQueue inDatabase:^(FMDatabase *db) {
    // 清除数据
    NSLog(@"-- add Station begin count = %ld",stations.count);
    [db beginTransaction];
    [db executeUpdate:@"delete from sgv_station where city_code = ?",@(cityCode)];
    [stations enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SGVSubwayStation * station = obj;
        [db executeUpdate:@"INSERT INTO sgv_station (city_code, line_code, station_code, station_name, line_inner_code, is_wifi_valid) VALUES (?,?,?,?,?,?)",@(cityCode), @(station.lineCode),@(station.stationCode), station.stationName, @(station.lineInnerCode),@(station.isWiFiValid)];
    }];
    [db commit];
    NSLog(@"-- add Station end"];
}];

相关文章

  • FMDB性能优化-使用事务提升性能

    使用FMDB事务批量更新数据,速度会有大幅度提升。 下面的例子中可以看到,不使用事务,更新374条数据就用了6秒多...

  • FMDB性能提升

    使用FMDB的过程中,性能的提升算一个技术点。 使用cache 既然是性能我们首先想到的是缓存cache缓存的话,...

  • 相机开发事宜

    性能问题 相机启动速度,需要持续优化。 使用Camera2 API,底层使用hal3接口,据说会有很大性能提升。 ...

  • 2020 前端 React 面试

    性能优化 性能优化,永远是面试的重点,性能优化对于 React 更加重要 在页面中使用了setTimout()、a...

  • react性能优化

    React 性能优化 React 性能优化 | 包括原理、技巧、Demo、工具使用[https://juejin....

  • React-Redux性能优化

    前面写了两篇文章《React组件性能优化》《Redux性能优化》,分别针对React和Redux在使用上的性能优化...

  • UITableView性能优化

    1.性能优化-UITableView的优化使用 2.老生常谈之UITableView的性能优化

  • 简述http缓存

    简介 网站性能第一优化定律:优先考虑使用缓存优化性能。合理的使用缓存,对网站的性能优化的意义重大。以下对于缓存,都...

  • Android性能优化 - 消除卡顿

    性能优化系列阅读 Android性能优化 性能优化 - 消除卡顿 性能优化 - 内存优化 性能分析工具 - Tra...

  • Android性能优化 - 内存优化

    性能优化系列阅读 Android性能优化 性能优化 - 消除卡顿 性能优化- 内存优化 性能分析工具 - Trac...

网友评论

  • 大大盆子:使用事务为什么能提高效率呢? 事务不是为了保持其中操作的完整性么?
    qiushuitian:我的理解是,事务在commit的时候让数据操作一次性写文件。而不是每次操作都写文件。

本文标题:FMDB性能优化-使用事务提升性能

本文链接:https://www.haomeiwen.com/subject/ycntrxtx.html