Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7988193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:21:58+00:00 2026-06-04T12:21:58+00:00

In my viewWillAppear callback I attempt to populate a UITableViewController with data from SQLite

  • 0

In my “viewWillAppear” callback I attempt to populate a UITableViewController with data from SQLite database for the user to see.

However, what I noticed was if I switch to another tab, commit a new row of data into SQLite and switch back to the UITableViewController it does not update with the new row I just added to the database. I have to quit out of the app completely and navigate back to the UITableViewController in order to see the new row reflected on the table view.

How do I get around this problem (i.e. how do I force always showing the very latest information in SQLite on the UITableViewController after switching back and forth a bunch of times?)

Would appreciate all / any advice.

Here is the code:

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

}

Also in case this is relevant, here is the code that commits to the SQLite database:

    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);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:22:00+00:00Added an answer on June 4, 2026 at 12:22 pm

    As an extension of an explanation of Darren’s answer.

    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

    The steps you should be taking to ensure consistency are the following:

    Loading Data into Table

    • 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

    Saving Data into Database

    • 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

    Using the NSMArray to ensure consistency between updates and app loads is fairly common practise has be recommended to me in the past by fellow co workers with decades of experience.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got one problem with the UITableViewController... The viewWillAppear method is not getting
I have a couple of UITableViewController classes and I just noticed that these methods
In the template Apple gives you, I see that they have: /* - (void)viewWillAppear:(BOOL)animated
I have a UIButton which calls a method from another UIViewController , that method
- (void)viewWillAppear:(BOOL)animated { app = [UIApplication sharedApplication]; app.networkActivityIndicatorVisible = YES; NSURL *url = [NSURL
I am having a strange problem: In -(void)viewWillAppear:(BOOL)animated method I use: [[UIApplication sharedApplication] setStatusBarHidden:YES
I schedule a timer with NSTimer's function in viewWillAppear as follows: minutesTimer = nil;
I want to show the UIBarButtonItem and UINavigationItem through code in the method viewWillAppear
i came across this problem, my code in the viewWillAppear method is not executed
I have UIScrollView with multiple UIVIew subviews. I would like to update the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.