iphone - 如何在Objective-C中提高这种sqlite数据库查询/对象创建的速度?

标签 iphone sqlite nsmutablearray while-loop

我有以下方法在iPhone上查询sqlite db表
它从表中提取约6,000行,然后根据信息循环遍历那些创建6,000个对象并将它们填充到数组中。

while循环有些慢(在我的设备上迭代它们大约需要10秒钟)
对如何加快速度有任何想法吗?

TKS !!!

NSMutableArray  *dees              = [[NSMutableArray alloc] init];

if(sqlite3_open([database_path UTF8String], &database) == SQLITE_OK) {
  const char      *sqlStatement = "select cn, gn, df, or, id from doits order by lower(cn)";
  sqlite3_stmt    *compiledStatement;
  if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {

    //loop through the results and feed them into the array
    while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
        // Read the data from the result row
        NSString    *a_cn             = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
        NSString    *a_gn             = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
        NSString    *a_df             = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
        NSString    *an_or            = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
        NSString    *ident            = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];

        DL   *dl = [[DL alloc] init];
        dl.cn            = a_cn;
        dl.gn            = a_gn;
        dl.df            = a_df;
        dl.or            = an_or;
        dl.primary_id    = [ident intValue];
        [dees      addObject:dl];
    }
}
sqlite3_finalize(compiledStatement);

最佳答案

为什么要在运行时创建所有6K?延迟加载它们并仅在需要时创建所需的项目怎么样?

iPhone不是台式机,它的400 + MHz CPU和6K的迭代速度将很慢。

您的表索引正确吗?您能否优化架构(字段类型?)?除了迭代6K元素外,您实际上没有做太多事情-因此我看不到优化该循环的直接方法。

我会看大图并尝试找到一种方法来延迟加载或部分加载数据。假设您使用数据填充UITableView,也许您只需要第一层,而不是完全成熟的对象?

编辑:

我所说的字段类型是在数据库中有4个“ sqlite3_column_text”文本字段。您是否必须使用文本字段,或者是否可以使用较小且可能更合适的数据?

关于iphone - 如何在Objective-C中提高这种sqlite数据库查询/对象创建的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2259992/

相关文章:

iphone - Xcode 8 : Unity 4. 6.9 - 不允许删除 Unity 启动画面

iphone - 为独立开发者设置 itunes connect

database - SQLite中获取排他锁的函数

xcode - 如何对二维多维 NSMutablearray 进行排序

iphone - 使用 Google map 的火车轨道路线

iphone - 使用 presentsWithGesture 时未调用 UISplitViewControllerDelegate 方法?

ios - 从 NSArray 中移除对象

ios - 使用 NSString 变量的名称创建可变数组

c# - .NET c# 如何使用 linq2db 批量插入?

mysql - Delphi Firemonkey XE8 - 如何使用 DataSnap 正确发送/接收图像