Search code examples
iphonexcodesqlitefmdb

iphone database using sqlite and fmdb wrapper - slow UI


I've created a database for the iphone using sqlite and the fmdb wrapper.

The database has roughly 3500 rows in it. It has 14 columns: 1 for an id, 1 for a name/main search term, 9 other columns for alternative search terms and 3 other columns for short descriptions.

My app's first view is just an introductory screen displaying a search bar, a bit like google. After implementing the search bar, it will bring you to another view containing a table featuring the desired results. There is a 1 or 2 second delay in this initial transition between views. Furthermore, the table doesn't allow for seamless scrolling. Finally, when you select a table cell, it brings you to a final view seamlessly, but when you try to return to the table view, there is another 1 or 2 second delay.

I've run this app with a smaller database that has 3500 rows but only 5 columns. In this case, the 9 alternative search terms are deleted from the database. When I run the app like this on the iphone, it is rather efficient... there is a small delay but it really isn't noticeable... I probably wouldn't have picked up on the lag, (I would just have assumed that the small delay is normal), if it wasn't so apparent with the larger database that I use.

I've come to the conclusion that the sqlite database needs some tweaking and thus, I have two main questions.

  1. Ive read that changing pragma settings, (ex: synchronous, journal mode, cache size), for the sqlite db will increase efficiency. When I change these settings via the mozilla plugin, they don't seem to save... I will open and close the plugin and the settings will revert to the old defaults. I saw that you can set these via xcode... i am wondering where in the FMDB wrapper to set these settings?
  2. I've heard that indexing greatly can increase speed. I don't really know what to index though, and if I do index something, do I have to make changes in the coding of the FMDB wrapper? Furthermore, what is it that I should index considering in an ideal situation I'd like to use all the data in my database, (albeit some columns I will use differently than others)?

-(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;
    }

    }

Solution

  • The cellForRowAtIndexPath is called for each cell created, so your filterDataWithName function is being called for every cell/row.

    Call your filterDataWithName somewhere else, and just use the NSMutableArray in cellForRowAtIndexPath with something like

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