以下代码:
-(NSArray *)getContentsWithContextTypes:(NSArray *)contextTypes
contextData:(NSArray *)contextData
{
__block NSString *query = @"SELECT * FROM Texts_original1 WHERE ";
[contextData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *attributeName = [self.contextTypeToDBQueryTexts
objectForKey:contextTypes[idx]];
query = [query stringByAppendingFormat:@"%@ = \"%@\"", attributeName, obj];
if(idx != contextData.count - 1)
{
query = [query stringByAppendingString:@" AND "];
}
}];
[self.db open];
FMResultSet *results = [self.db executeQuery:query];
NSMutableArray *array = [NSMutableArray array];
while([results next])
{
Content *content = [[TextualContent alloc] initWithResults:results];
[array addObject:content];
}
[self.db close];
return array;
}
运行时生成以下错误:
调用sqlite3_step(21:内存不足)rs
时出错
它发生在循环的一半。应该有33个结果。 17之后我得到了这个错误并且循环退出。有任何想法吗?感谢。
答案 0 :(得分:6)
了解您关闭 FMResultSet对象非常重要,因为它会导致内存问题。
按照以下方式执行,[resultSet close]
我没有进入您的代码逻辑,但是,我将在数据库代码中进行一些修改。
尝试以下方法:
-(NSArray *)getContentsWithContextTypes:(NSArray *)contextTypes
contextData:(NSArray *)contextData
{
__block NSString *query = @"SELECT * FROM Texts_original1 WHERE ";
[contextData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
{
NSString *attributeName = [self.contextTypeToDBQueryTexts
objectForKey:contextTypes[idx]];
query = [query stringByAppendingFormat:@"%@ = \"%@\"", attributeName, obj];
if(idx != contextData.count - 1)
{
query = [query stringByAppendingString:@" AND "];
}
}];
self.db = [FMDatabase databaseWithPath:[self getDBPath]];
if(![db open])
{
NSLog(@"Could not open DB");
return nil;
}
FMResultSet *results = [self.db executeQuery:query];
NSMutableArray *array = [NSMutableArray array];
while([results next])
{
Content *content = [[TextualContent alloc] initWithResults:results];
[array addObject:content];
}
[results close]; //VERY IMPORTANT!
[self.db close];
return array;
}