I have a problem here. My app saves some objects into a SQLite database and that database is the source for my UITableView. Everything works fine, except if a add a new object to my database at runtime, my UITableView doesn´t update its content. Here´s my code:
- (void)viewDidLoad
{
[self daten];
[super viewDidLoad];
}
-(NSMutableArray *) daten{
if (daten)
return daten;
self.daten = [[NSMutableArray alloc] init];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [self getDBPath];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
}
const char *sql = "SELECT * FROM Daten";
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}else{
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
.......
}
}
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
@finally {
sqlite3_finalize(sqlStatement);
sqlite3_close(db);
return daten;
}
}
I think the problem is here:
if (daten)
return daten;
self.daten = [[NSMutableArray alloc] init];
But if my database gets a new object at runtime, my array is not nil at that moment and will be just returned without the new object!
It’s simple, just add :-