iOS sql读/写内存

时间:2013-12-05 18:35:18

标签: ios sqlite memory

我有一个项目需要在其中使用sqlite。

我还创建了一个单例对象来管理我的sql

但我发现当我在sql中读取数据时,内存会不断增长,而且似乎永远不会释放。

以下代码是我如何读取单件对象中的数据

- (NSArray*)loadRecordPathData{
@synchronized(self)
{
    if(sqlite3_open([FILEPATH UTF8String], &database) == SQLITE_OK)
    {
        NSMutableArray *pathNames = [[NSMutableArray alloc] init];
        NSString *query = @"SELECT * FROM pathData";
        sqlite3_stmt *statement;
        if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil)
            == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {
                BBRecordPathData *recordPathData = [BBRecordPathData new];
                int readed = (int) sqlite3_column_int(statement, 0);
                int isFavorite = (int) sqlite3_column_int(statement, 1);
                char *pathDescroption = (char *) sqlite3_column_text(statement, 2);
                char *pathStopTime = (char *) sqlite3_column_text(statement, 3);
                char *pathStartTime = (char *) sqlite3_column_text(statement, 4);
                int pathChallange = (int) sqlite3_column_int(statement, 5);
                char *pathName = (char *) sqlite3_column_text(statement, 6);

                NSString *path_StartTime = (pathStartTime) ?[[NSString alloc] initWithUTF8String:pathStartTime] : nil;
                NSString *path_StopTime = (pathStopTime) ? [[NSString alloc] initWithUTF8String:pathStopTime] : nil;
                NSString *path_Name = (pathName) ? [[NSString alloc] initWithUTF8String:pathName]:nil;
                NSString *path_description = (pathDescroption) ? [[NSString alloc] initWithUTF8String:pathDescroption]: nil;

                recordPathData.recordPathName = path_Name;
                recordPathData.recordPathStartTime = path_StartTime;
                recordPathData.recordPathStopTime = path_StopTime;
                recordPathData.recordPathDescription = path_description;
                recordPathData.challange = pathChallange;
                recordPathData.isFavorite = isFavorite;
                recordPathData.readed = readed;
                //get contain locations
                recordPathData.cotainLocations = [self getLocationsPointFromStartTimeTag:recordPathData.recordPathStartTime]; //<----from another table
                if (recordPathData.cotainLocations.count == 0 || recordPathData.cotainLocations.count < 4) {
                    recordPathData.distance = @"0.000KM";
                }else{
                    NSString *unite = @"M";
                    float distanceInMeter = 0.0;
                    for (int i = 3; i < recordPathData.cotainLocations.count; i++) {
                        if (i <= recordPathData.cotainLocations.count - 2){
                            CLLocation *startPoint = recordPathData.cotainLocations[i];
                            CLLocation *endPoint = recordPathData.cotainLocations[i + 1];
                            distanceInMeter += [startPoint distanceFromLocation:endPoint];
                        }else{
                            recordPathData.distance = @"0.000KM";
                        }
                    }
                    if (distanceInMeter > 1000.0) {
                        unite = @"KM";
                        distanceInMeter /= 1000.0;
                        recordPathData.distance = [NSString stringWithFormat:@"%.2f%@",distanceInMeter,unite];
                    }else{
                        recordPathData.distance = [NSString stringWithFormat:@"%i%@",(int)distanceInMeter,unite];
                    }
                }

                [pathNames addObject:recordPathData];
            }
            sqlite3_finalize(statement);
            sqlite3_close(database);
        }

        return pathNames;
    }
    return nil;
}

}

任何建议或建议都将是一个很大的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

如果您正在使用Objective C中的SQLite,则应使用包装器,例如FMDatabase。它将为您处理内存管理并确保您不会泄漏。