在我的 iPhone 应用程序上,我使用的是 SQLite 数据库,为此我依赖fmdb。在装有 iOS 5 的 iPhone 上,初始 DB 访问最多需要 10 秒,但在 iOS 6 上需要 30 到 2 分 40 秒(这在 iPhone 4 和 4S 上,在 iPhone 5 上大约需要 15-20 秒)。我注意到的一件事是时间总是一致的,如果在一部手机上需要 60 秒,它总是需要大约 60 秒。
是否有任何理由在 iOS 6 中使用 fmdb 比在 iOS 5 上慢?有没有更好的替代 fmdb 的方法?
在代码方面,这是我正在做的一个示例:
获取数据
+ (NSMutableArray*) getLevadas
{
FMDatabase *db = [DBAdapter getDB];
if([db open])
{
FMResultSet *result = [db executeQuery:@"SELECT Levada.Levada_id, Levada.Nome, a.Nome as Inicio, b.Nome as Fim, Dificuldade.NomePT as DificuldadePT, Dificuldade.NomeES as DificuldadeES, Dificuldade.NomeEN as DificuldadeEN, Altitude, Distancia, DescricaoPT, DescricaoEN, DescricaoES, Latitude, Longitude, Autocarro, Levada.dataModificado, a.Sector, Levada.Duracao, b.Sector as SectorFim, GROUP_CONCAT(DISTINCT PalavrasChave.NomePT) as PalavrasChavePT, GROUP_CONCAT(DISTINCT PalavrasChave.NomeEN) as PalavrasChaveEN, GROUP_CONCAT(DISTINCT PalavrasChave.NomeES) as PalavrasChaveES, GROUP_CONCAT(DISTINCT Equipamento.NomeEN) as EquipamentoEN, GROUP_CONCAT(DISTINCT Equipamento.NomePT) as EquipamentoPT, GROUP_CONCAT(DISTINCT Equipamento.NomeES) as EquipamentoES, Levada.Coordenadas, Levada.LongitudeFim, Levada.LatitudeFim, Levada.PontosInteresse, GROUP_CONCAT(DISTINCT Fotos.url) as Fotos FROM Levada, Localizacoes as a, Localizacoes as b, Dificuldade, Levada_has_PalavrasChave, PalavrasChave, Levada_has_Equipamento, Equipamento, Fotos WHERE ((Inicio_id = a.Localizacoes_id) AND (Fim_id = b.Localizacoes_id) AND (Levada.Dificuldade_id = Dificuldade.Dificuldade_id) AND (Levada.Levada_id = Levada_has_PalavrasChave.Levada_id) AND (PalavrasChave.PalavrasChave_id = Levada_has_PalavrasChave.PalavrasChave_id) AND (Levada.Levada_id = Levada_has_Equipamento.Levada_id) AND (Levada_has_Equipamento.Equipamento_id = Equipamento.Equipamento_id) AND Levada.Levada_id = Fotos.idLevada) GROUP BY Levada.Levada_id ORDER BY Levada.Nome;"];
NSMutableArray *levadas = [[NSMutableArray alloc] init];
while ([result next])
{
Levada *l = [[Levada alloc] init];
[l ID:[NSNumber numberWithInt: [result intForColumn:@"Levada_id"]]];
[l Nome:[result stringForColumn:@"Nome"]];
[l Dificuldade:[result stringForColumn:[NSString stringWithFormat:@"Dificuldade%@", NSLocalizedString(@"lingua", NULL)]]];
[l Distancia:[result objectForColumnName:@"Distancia"]];
[l Duracao:[result stringForColumn:@"Duracao"]];
[l Inicio:[result stringForColumn:@"Inicio"]];
[l Fim:[result stringForColumn:@"Fim"]];
[l Altitude:[result stringForColumn:@"Altitude"]];
[l PalavrasChave:[[result stringForColumn:[NSString stringWithFormat:@"PalavrasChave%@", NSLocalizedString(@"lingua", NULL)]] stringByReplacingOccurrencesOfString:@"," withString:@", "]];
[l Descricao:[result stringForColumn:[NSString stringWithFormat:@"Descricao%@", NSLocalizedString(@"lingua", NULL)]]];
[l Fotos: [[result stringForColumn:@"Fotos"] componentsSeparatedByString:@","]];
[l Latitude:[NSNumber numberWithDouble:[[result stringForColumn:@"Latitude"] doubleValue]]];
[l LatitudeFim:[NSNumber numberWithDouble:[[result stringForColumn:@"LatitudeFim"] doubleValue]]];
[l Longitude:[NSNumber numberWithDouble:[[result stringForColumn:@"Longitude"] doubleValue]]];
[l LongitudeFim:[NSNumber numberWithDouble:[[result stringForColumn:@"LongitudeFim"] doubleValue]]];
[l Coordenadas:[result stringForColumn:@"Coordenadas"]];
[l Equipamento: [[result stringForColumn:[NSString stringWithFormat:@"Equipamento%@", NSLocalizedString(@"lingua", NULL)]] componentsSeparatedByString:@","]];
[l DataModificado:[result stringForColumn:@"dataModificado"]];
[l Sector: [result stringForColumn:@"Sector"]];
[l SectorFim: [result stringForColumn:@"SectorFim"]];
[l Autocarro: [result stringForColumn:@"Autocarro"]];
[levadas addObject:l];
}
[result close];
[db close];
return levadas;
}
return nil;
}
打开数据库
+ (FMDatabase*) getDB
{
[DBAdapter copyDatabaseIfNeeded];
FMDatabase *db = [FMDatabase databaseWithPath:[DBAdapter getDBPath]];
[db setLogsErrors:YES];
return db;
}
+ (void) copyDatabaseIfNeeded
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [DBAdapter getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"WalkMe.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
}
}
+ (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"WalkMe.sqlite"];
}
更新:
这是时间分析的屏幕截图:
据我所知,最耗时的是 [FMResultSet next] 指令,更具体地说是rc = sqlite3_step([_statement statement]);
在 FMDB 下一个代码内部。所以我猜问题真的是 SELECT 语句。
更新 2:
我试图将 SQL 语句简化为[FMResultSet next] Always clean your projectSELECT Levada_id FROM Levada
中仍然需要太多时间。