When writing to text file (appending) the last value I write replaces all previous values When writing to text file (appending) the last value I write replaces all previous values sqlite sqlite

When writing to text file (appending) the last value I write replaces all previous values


You're using always the same instance of GPS in the repeat loop so due to reference semantics the data in the object are overwritten and the same object is added to the array in each iteration.

Move the line

GPS *gps = [[GPS alloc] init];

just before the line

[gps setJobNo:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]];

to create a new GPS instance in each iteration


Create a new instance of the gps-object in every iteration:

while(sqlite3_step(statement) == SQLITE_ROW) {     gps = [[GPS alloc] init];     [gps setJobNo:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]];     [gps setSourceMonitor:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]];     [gps setPositionNo:[NSNumber numberWithInt:sqlite3_column_int(statement,2)]];     [gps setLatitude:sqlite3_column_double(statement, 3)];     [gps setLongitude:sqlite3_column_double(statement, 4)];     NSLog(@"Source Monitor: %@", [gps sourceMonitor]);     [allGPS addObject:gps]; }