1

我使用 sqlite 和 fmdb 包装器为 iphone 创建了一个数据库。

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

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

我用一个较小的数据库运行这个应用程序,该数据库有 3500 行但只有 5 列。在这种情况下,从数据库中删除了 9 个备选搜索词。当我在 iphone 上运行这样的应用程序时,它是相当有效的......有一个小的延迟,但它真的不明显......我可能不会因为滞后而受到影响,(我只会有假设小的延迟是正常的),如果我使用的更大的数据库不是那么明显的话。

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

  1. 我已经读过更改 sqlite db 的编译指示设置(例如:同步、日志模式、缓存大小)将提高效率。当我通过 mozilla 插件更改这些设置时,它们似乎没有保存...我将打开和关闭插件,设置将恢复为旧的默认值。我看到你可以通过 xcode 设置这些...我想知道 FMDB 包装器在哪里设置这些设置?
  2. 我听说索引可以大大提高速度。我真的不知道要索引什么,如果我索引某些东西,我是否必须更改 FMDB 包装器的编码?此外,在理想情况下我应该索引什么我想使用我的数据库中的所有数据(尽管我将使用与其他列不同的某些列)?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static 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;
    }

    }
4

2 回答 2

1

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

在其他地方调用您的 filterDataWithName ,然后将 cellForRowAtIndexPath 中的 NSMutableArray 与类似的东西一起使用

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

于 2012-02-01T15:55:16.493 回答
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];

}

I recommend egodatabase which has non blocking gcd (grand central dispatch) code https://github.com/jdp-global/egodatabase

于 2012-03-13T09:00:18.580 回答