2012-01-31 89 views
1

我已经创建了一个数据库为iphone使用sqlite和fmdb包装。iphone数据库使用sqlite和fmdb包装 - 慢UI

数据库中大约有3500行。它有14列:1为id,1为名称/主要搜索词,9个其他列用于替代搜索词和3个其他列用于简短描述。

我的应用程序的第一个视图只是一个介绍性屏幕,显示一个搜索栏,有点像谷歌。在执行搜索栏之后,它会将您带到另一个包含具有所需结果的表格的视图。视图之间的初始转换有1或2秒延迟。此外,该表格不允许无缝滚动。最后,当你选择一个表格单元格时,它将无缝地引导你到最后的视图,但是当你尝试返回表格视图时,会有另一个1或2秒的延迟。

我已经运行这个应用程序与一个较小的数据库,有3500行,但只有5列。在这种情况下,从数据库中删除9个备选搜索条件。当我在iPhone上运行这样的应用程序时,它效率很高......有一个小延迟,但它确实不明显......我可能不会在延迟上捡起来的,(我只会假设小的延迟是正常的),如果它对于我使用的更大的数据库不那么明显。

我得出结论,sqlite数据库需要一些调整,因此,我有两个主要问题。

  1. 我读过改变杂注设置(例如:同步,日记模式,缓存大小),为sqlite数据库将提高效率。当我通过mozilla插件更改这些设置时,它们似乎不能保存...我将打开并关闭插件,设置将恢复为旧的默认设置。我看到你可以设置这些通过Xcode ...我想知道在FMDB包装设置这些设置在哪里?
  2. 我听说索引大大可以提高速度。我真的不知道要索引什么,如果我做了索引,我是否必须对FMDB封装的编码进行更改?此外,我应该在理想情况下考虑索引什么,我想使用数据库中的所有数据(尽管有些列的使用方式与其他列不同)?

- (的UITableViewCell *)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态的NSString * CellIdentifier = @ “CustomCell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    Data *data = [[[DataController instance]filterDataWithName:searchString:searchString:searchString:searchString:searchString:searchString:searchString:searchString:searchString:searchString]objectAtIndex:indexPath.row]; 

    cell.textLabel.text = data.aDataName; 
    cell.detailTextLabel.text = data.aDataStatus; 

    // Configure the cell. 
    return cell; 
} 

-(NSMutableArray*)filterDataWithName:(NSString*)aDataName:(NSString*)altSearchTermA:(NSString*)altSearchTermB:(NSString*)altSearchTermC:(NSString*)altSearchTermD:(NSString*)altSearchTermE:(NSString*)altSearchTermF:(NSString*)altSearchTermG:(NSString*)altSearchTermH:(NSString*)altSearchTermI; 

{ 

    if ((aDataName && [aDataName length] > 0) && (altSearchTermA && [altSearchTermA length] > 0) && (altSearchTermB && [altSearchTermB length] > 0) && (altSearchTermC && [altSearchTermC length] > 0) && (altSearchTermD && [altSearchTermD length] > 0) && (altSearchTermE && [altSearchTermE length] > 0) && (altSearchTermF && [altSearchTermF length] > 0) && (altSearchTermG && [altSearchTermG length] > 0) && (altSearchTermH && [altSearchTermH length] > 0) && (altSearchTermI && [altSearchTermI length] > 0)) 
    { 
     NSMutableArray *filterDataArray = [[NSMutableArray alloc]initWithArray:dataList]; 

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(aDataName CONTAINS[cd] %@) OR (altSearchTermA CONTAINS[cd] %@) OR (altSearchTermB CONTAINS[cd] %@) OR (altSearchTermC CONTAINS[cd] %@) OR (altSearchTermD CONTAINS[cd] %@) OR (altSearchTermE CONTAINS[cd] %@) OR (altSearchTermF CONTAINS[cd] %@) OR (altSearchTermG CONTAINS[cd] %@) OR (altSearchTermH CONTAINS[cd] %@) OR (altSearchTermI CONTAINS[cd] %@)", aDataName, altSearchTermA,altSearchTermB,altSearchTermC,altSearchTermD,altSearchTermE,altSearchTermF,altSearchTermG,altSearchTermH,altSearchTermI]; 

     [filterDataArray filterUsingPredicate:predicate]; 
     return filterDataArray; 
    } 
    else 
    { 
     return dataList; 
    } 

    } 
+0

你如何填充你的表视图?您是否一次查询所有记录,创建对象并将其保存在内存中,或者在需要时查询每行的数据库(滚动表视图)?索引是一个好主意,如果你做这样的搜索。 – lawicko 2012-01-31 15:12:19

+0

@lawicko - 我将整个sqlite数据库导入到一个巨型数组中,并从那里我用NSPredicate搜索过滤它...我已经设置好NSPredicate函数根据搜索创建它自己的数组这个数组就是填充我的表的东西。 – 2012-01-31 19:47:46

+0

好吧,在这种情况下,我怀疑索引可以帮助你,因为你无论如何都从db中获取所有东西。让我看看你的* cellForRow * ...方法,并使用时间模板来分析你的应用程序,看看从一个视图转换到另一个视图时,这2秒是什么。 – lawicko 2012-02-01 10:14:08

回答

1

为每个创建的单元格调用cellForRowAtIndexPath,因此将为每个单元格/行调用filterDataWithName函数。

打电话给你filterDataWithName别的地方,只是使用的NSMutableArray中的cellForRowAtIndexPath喜欢的东西

cell.textLabel.text = [[filterDataArray objectAtIndex:indexpath.row] data.aDataName]。

+0

感谢您的帮助,我真的很感激它,这个修复工作! – 2012-02-01 16:32:22

0

好的 - 我错过了Darren提到的上述问题 - 如果这不能解决问题.....我遇到了类似的问题,我需要在桌面上进行详尽的搜索。你可以试着用这样的东西来限制你的查询。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Level2CustomCell *cell = (Level2CustomCell*) [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[Level2CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    // SEARCH ENGINE TIME 
    NSString *likeTitle = [NSString stringWithFormat:@" TITLE LIKE '%@%@%@'",@"%",msgView.searchBar.text,@"%"]; 
    NSString *likeSearch = [NSString stringWithFormat:@" SORT4='%@' AND SEARCH LIKE '%@%@%@'",selectedSection,@"%",msgView.searchBar.text,@"%"]; 
    NSString *likeAddress = [NSString stringWithFormat:@" SORT4='%@' AND ADDRESS LIKE '%@%@%@'",selectedSection,@"%",msgView.searchBar.text,@"%"]; 
    NSString *likeContent = [NSString stringWithFormat:@" SORT4='%@' AND CONTENT LIKE '%@%@%@'",selectedSection,@"%",msgView.searchBar.text,@"%"]; 


    qry = [NSString stringWithFormat: @"select * from SPOT_INFO WHERE SORT4 = '%@' AND %@ OR %@ OR %@ OR %@ order by TITLE limit 1 offset %d",selectedSection,likeTitle,likeSearch,likeAddress,likeContent,[indexPath row]]; 


    DLog(@"qry :%@",qry); 

    FMResultSet *rs = [appDelegate.userdb executeQuery:qry]; 

    if ([rs next]) { 
     NSString *title = [rs stringForColumn:@"TITLE"]; 
     DLog(@"title %@",title); 
     NSString *content = [rs stringForColumn:@"CONTENT"]; 
     NSString *imgFileName = [rs stringForColumn:@"IMG_NAME"]; 

    } 
    [rs close]; 

} 

我建议egodatabase具有无阻塞GCD(盛大中央调度)代码 https://github.com/jdp-global/egodatabase