簡體   English   中英

使用sqlite和fmdb包裝器的iPhone數據庫-緩慢的UI

[英]iphone database using sqlite and fmdb wrapper - slow UI

我已經使用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 {靜態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;
    }

    }

將為創建的每個單元格調用cellForRowAtIndexPath,因此將為每個單元格/行調用filterDataWithName函數。

在其他地方調用filterDataWithName,然后在cellForRowAtIndexPath中使用NSMutableArray,例如

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

好的-我錯過了達倫(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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM