简体   繁体   中英

iOS sql read/write memory

I have a project needs to use the sqlite in it.

And I also create a singleton object to manage my sql

But I just found out that when I read data in sql,the memory just keep growing,and it seems never release.

the following code is how I read data in my singleton object

- (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;
}

}

any suggestion or advice will be a great help , thanks!

If you're using SQLite from Objective C, you should use a wrapper, such as FMDatabase. It will handle the memory management for you and ensure you don't leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM