0

以下代码:

-(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 之后我得到那个错误并且循环退出。有任何想法吗?谢谢。

4

1 回答 1

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;

}

于 2012-08-28T07:25:43.600 回答