【发布时间】:2011-09-26 08:16:47
【问题描述】:
我正在创建一个字典应用程序,我正在尝试将术语加载到 iPhone 字典中以供使用。这些术语是从此表中定义的 (SQLite):
id -> INTEGER autoincrement PK
termtext -> TEXT
langid -> INT
normalized -> TEXT
使用归一化是因为我用希腊语写作,并且我在 sqlite 引擎上没有用于搜索变音符号的 icu,所以我使 termtext 变音符号/大小写不敏感。它也是主要的搜索字段,与可能是“视图”字段的 termtext 形成对比。
我已经定义了一个这样的类(比如 POJO):
terms.h
#import <Foundation/Foundation.h>
@interface Terms : NSObject {
NSUInteger termId; // id
NSString* termText; // termtext
NSUInteger langId; // langid
NSString* normalized; // normalized
}
@property (nonatomic, copy, readwrite) NSString* termText;
@property (nonatomic, copy, readwrite) NSString* normalized;
@property (assign, readwrite) NSUInteger termId;
@property (assign, readwrite) NSUInteger langId;
@end
terms.c
#import "Terms.h"
@implementation Term
@synthesize termId;
@synthesize termText;
@synthesize langId;
@synthesize normalized;
@end
现在在我的代码中,我使用FMDB 作为SQLite 数据库的包装器。我使用以下代码加载条款:
[... fmdb defined database as object, opened ]
NSMutableArray *termResults = [[[NSMutableArray alloc] init] autorelease];
FMResultSet *s = [database executeSQL:@"SELECT id, termtext, langid, normalized FROM terms ORDER BY normalized ASC"];
while ([s next]) {
Term* term = [[Terms alloc] init];
term.termId = [s intForColumn:@"id"];
[... other definitions]
[termResults addObject:term];
[term release];
}
然后将整个 termResults 加载到 UITableView(在 viewdidload 上),但每次启动我的应用程序时加载最多需要 5 秒。有什么方法可以加快这个过程吗?我在SQLite 上索引了id、termText 和规范化。
*** 更新:添加 cellForRowAtIndexPath ****
[.. standard cell definition...]
// Configure the cell
Term* termObj = [self.termResults objectAtIndex:indexPath.row];
cell.textLabel.text = termObj.termText;
return cell;
【问题讨论】:
-
您应该检查一下,但我认为 while 循环正在杀死您。是否有任何理由将您的结果移动到数组中?我假设您正在使用它来填充表格视图。如果是这种情况,那么只需在数据源中使用结果集对象。只要您知道如何取出您的对象,就没有什么说您需要为此使用数组。
-
真的有必要让所有这些对象在运行时都存在吗?你不能按需从数据库中加载它们并缓存它们吗?
-
你能给我们看看 cellForRowAtIndexPath 方法定义吗?
-
我也在使用这个数组作为 uisearchbarcontroller 的来源(进行过滤)。在我看到的示例(苹果)中,它使用数组进行搜索操作。
-
DarkDust:规范之一是让所有单词在运行时可用。 UISearchBarController 也使用相同的数组进行过滤(使用 nspredicate,速度很快)。所以整个问题是我将如何实现 10k+ 行的快速加载。关闭应用后 FMDB 缓存是否仍然有效?
标签: iphone sqlite optimization fmdb