[英]iphone database using sqlite and fmdb wrapper - slow UI
I've created a database for the iphone using sqlite and the fmdb wrapper. 我已经使用sqlite和fmdb包装器为iphone创建了一个数据库。
The database has roughly 3500 rows in it. 该数据库中大约有3500行。 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.
它有14列:1代表ID,1代表名称/主要搜索词,9列代表替代搜索词,3列代表简短描述。
My app's first view is just an introductory screen displaying a search bar, a bit like google. 我的应用的第一个视图只是一个介绍性屏幕,显示搜索栏,有点像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.
视图之间的初始过渡有1或2秒的延迟。 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.
最后,当您选择一个表格单元格时,它将无缝地带您进入最终视图,但是当您尝试返回到表格视图时,还会有1或2秒的延迟。
I've run this app with a smaller database that has 3500 rows but only 5 columns. 我已经使用具有3500行但只有5列的较小数据库运行了该应用程序。 In this case, the 9 alternative search terms are deleted from the database.
在这种情况下,将从数据库中删除9个替代搜索词。 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.
当我在iPhone上像这样运行应用程序时,它效率很高……虽然延迟很小,但实际上并不引人注目……我可能不会迟滞了,(我会假设较小的延迟是正常的),如果在我使用的较大数据库中不是那么明显。
I've come to the conclusion that the sqlite database needs some tweaking and thus, I have two main questions. 我得出的结论是,sqlite数据库需要进行一些调整,因此,我有两个主要问题。
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CustomCell"; -(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;
}
}
The cellForRowAtIndexPath is called for each cell created, so your filterDataWithName function is being called for every cell/row. 将为创建的每个单元格调用cellForRowAtIndexPath,因此将为每个单元格/行调用filterDataWithName函数。
Call your filterDataWithName somewhere else, and just use the NSMutableArray in cellForRowAtIndexPath with something like 在其他地方调用filterDataWithName,然后在cellForRowAtIndexPath中使用NSMutableArray,例如
cell.textLabel.text = [[filterDataArray objectAtIndex:indexpath.row] data.aDataName]; cell.textLabel.text = [[filterDataArray objectAtIndex:indexpath.row] data.aDataName];
Ok - I missed the problem above as mentioned by Darren - if this doesn't fix it ..... I encountered a similar problem where I needed to run an exhaustive search on table. 好的-我错过了达伦(Darren)提到的上述问题-如果不能解决该问题.....我遇到了类似的问题,需要在表上进行详尽的搜索。 You could try restricting your query to one row with something like this.
您可以尝试将查询限制为这样的一行。
- (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 我建议egodatabase具有不阻塞的gcd(中央中央调度)代码https://github.com/jdp-global/egodatabase
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.