I’m creating a SQLite application, with two simple views. I have two arrays in SQLAppDelegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
self.modelArray = tempArray;
self.detailArray = tempArray;
[tempArray release];
So now in my implementation file I am populating “modelArray” with the contents of a SQLite table and displaying this in the first view:
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
SQLAppDelegate *appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select rowid as ID, ManufacturerMake as modelName from sqlitedb group by modelName";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Model *modelObj = [[Model alloc] initWithPrimaryKey:primaryKey];
modelObj.modelName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
[appDelegate.modelArray addObject:modelObj];
[modelObj release];
}
}
}
The problem is that modelArray is being populated correctly with 114 rows, but if I do a count of detailArray after the above while statement this is also returning 114? Even though I’m not touching detailArray in the above code?
Here both your instance variable arrays contain pointer to the same NSMutableArray instance (assuming that your property has retain attribute), so changes to the one of the iVars are also applied to another one (as they point to the same object).
To fix that initialize your arrays with different NSMutableArray’s: