English 中文(简体)
UITableViewC主计长在重新启动应用程序后不总是与实际 SQLite 数据库保持一致
原标题:UITableViewController not always consistent with actual SQLite database until after restarting app

在我的“ViewWillAppear”回调中,我试图用 SQLite 数据库的数据填充一个UIT ViewCrenter, 供用户查看。

然而,我注意到的是,如果我转换到另一个标签,将一列新数据输入SQLite,然后转回UITableViewCtory,它不会与刚添加到数据库的新行同步更新。我必须完全退出应用程序,然后返回到UITVoctor,以便看到表格视图中反映的新行。

我该如何克服这个问题(即我该如何强迫总是在UITableViewC主计长的 SQLite 上显示最新信息?

感谢所有/任何建议。

这是代码:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"viewwillappear");
//[[self tableView] reloadData];
int rc=-1;
if (databasePath == nil) {
    NSLog(@"database path is NIL. Trying to set it");
    databaseName = @"mymemories.sqlite";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    return;
时 时
rc = sqlite3_open([databasePath UTF8String], &database);
if(rc == SQLITE_OK) {
    memoriesArray = [[NSMutableArray alloc]init ];
    sqlite3_stmt *statement = nil;


    NSString *fullQuery = @"SELECT * FROM memories";

    const char *sql = [fullQuery UTF8String];

    if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL)!=SQLITE_OK)
        NSAssert1(0, @"Error preparing statement  %s ", sqlite3_errmsg(database));
    else
    {
        while(sqlite3_step(statement) == SQLITE_ROW)
        {
            NSString *place= [NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 4)];
            //[User setName:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 1)]];
            //[User setAge:[NSString stringWithUTF8String:(const char*)sqlite3_column_text(statement, 2)]];

            [memoriesArray addObject:place];
            //[currentUser release];
        时 时
    时 时
    sqlite3_finalize(statement);
    sqlite3_close(database);
时 时

时 时

如果与此相关,这里是承诺输入SQLite数据库的代码:

    NSData * blob = [NSData dataWithContentsOfURL:recordedTmpFile];
int rc=-1;
rc = sqlite3_open([databasePath UTF8String], &database);

if(rc == SQLITE_OK) {
    sqlite3_exec(database, "BEGIN", 0, 0, 0);


    NSLog(@"Connected To: %@",databasePath);
    sqlite3_stmt *updStmt =nil; 
    const char *sql = "INSERT INTO memories (data,place) VALUES (?,?);";

    rc = sqlite3_prepare_v2(database, sql, -1, &updStmt, NULL);

    if(rc!= SQLITE_OK)
    {
        NSLog(@"Error while creating update statement:%@", sqlite3_errmsg(database));
    时 时
    sqlite3_bind_text( updStmt, 2, [[tags text] UTF8String], -1, SQLITE_TRANSIENT);
    rc = sqlite3_bind_blob(updStmt, 1, [blob bytes], [blob length] , SQLITE_BLOB);

    if((rc = sqlite3_step(updStmt)) != SQLITE_DONE)
    {
        NSLog(@"Error while updating: %@", sqlite3_errmsg(database));
        sqlite3_reset(updStmt);
    时 时 

    sqlite3_exec(database, "COMMIT", 0, 0, 0);
    //rc = sqlite3_reset(updStmt);

    sqlite3_close(database);

时 时
最佳回答

作为对Darren答复的解释的延伸。

First off his answer is correct. Secondly you need to use an NSMutableArray to ensure consistency, this is where you are going wrong by not updating it as you should

为确保一致性,你应当采取的步骤如下:

<强度 > 将数据输入表

  • In viewDidLoad, call your SQL statement and load it into your array
  • in viewWillAppear ensure that your array contains data, if not display a notice that no results were returned

<强度> 将数据保存到数据库

  • Update the change to the array (or datasource)
  • Update the Database with the updated datasource to ensure consistency
  • Update the table with one of the 4 UITableView reloading methods

过去曾有数十年经验的同业工人向我推荐使用NSMArray, 以确保更新与应用程序负荷之间的一致性。

Note: You will need to synchronise the datasource to ensure that 1 thread is accessing it at any 1 time otherwise you will get a crash.

问题回答

假设您将 SQL 数据读入一个数组,然后使用此数组来构建 UITView,当您在 SQL 数据库中添加一个记录时,您需要将其添加到用于构建表格的数组中,或者将数据库中的数据重新读入数组中。





相关问题
sqlite3 is chopping/cutting/truncating my text columns

I have values being cut off and would like to display the full values. Sqlite3 -column -header locations.dbs " select n.namelist, f.state, t.state from names n left join locations l on l.id = n.id ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop ...

Metadata for columns in SQLite v2.8 (PHP5)

How can I get metadata / constraints (primary key and "null allowed" in particular) for each column in a SQLite v2.8 table using PHP5 (like mysql_fetch_field for MySql)? sqlite_fetch_column_types (OO:...

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签