iPhone Appのサンプルコード SQLiteBooksのソースを読む - 3. 入力データの保存処理を確認
入力画面
Book Listの「+」ボタンを押下すると入力画面が表示される。
「Save」ボタン押下時に実行されるメソッドは?
DBへのインサートはBookのinsertIntoDatabaseメソッドで処理しているようだ。
insertIntoDatabaseを呼んでいるのは、AddViewControllerのsaveメソッド。
実際にそのように動いているのかデバッグ文を追加し確認してみる。
Book.m
- (void)insertIntoDatabase:(sqlite3 *)db { NSLog(@"Book#insertIntoDatabase") ; database = db; // This query may be performed many times during the run of the application. As an optimization, a static // variable is used to store the SQLite compiled byte-code for the query, which is generated one time - the first // time the method is executed by any Book object. if (insert_statement == nil) { static char *sql = "INSERT INTO book (title) VALUES(?)"; if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK) { NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database)); } } sqlite3_bind_text(insert_statement, 1, [title UTF8String], -1, SQLITE_TRANSIENT); int success = sqlite3_step(insert_statement); // Because we want to reuse the statement, we "reset" it instead of "finalizing" it. sqlite3_reset(insert_statement); if (success == SQLITE_ERROR) { NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database)); } else { // SQLite provides a method which retrieves the value of the most recently auto-generated primary key sequence // in the database. To access this functionality, the table should have a column declared of type // "INTEGER PRIMARY KEY" primaryKey = sqlite3_last_insert_rowid(database); } // All data for the book is already in memory, but has not be written to the database // Mark as hydrated to prevent empty/default values from overwriting what is in memory hydrated = YES; }
AddViewController.m
- (IBAction)save:(id)sender { NSLog(@"AddViewController#save") ; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; // Add the book to the global array of books [appDelegate addBook:self.book]; // Dismiss the modal view to return to the main list [self.navigationController dismissModalViewControllerAnimated:YES]; }
デバッッグコンソールの出力
「New Book」画面でデータを入力し「Save」ボタンを押下した後のデバッグコンソールの出力は下記のようになった。
AddViewController#save、Book#insertIntoDatabaseが呼ばれているので思ったとおりの動作になっている。
2008-11-30 19:42:46.523 SQLiteBooks[28013:20b] AddViewController#save 2008-11-30 19:42:46.523 SQLiteBooks[28013:20b] Book#insertIntoDatabase 2008-11-30 19:42:46.531 SQLiteBooks[28013:20b] MasterViewController#tableView cellForRowAtIndexPath 2008-11-30 19:42:46.532 SQLiteBooks[28013:20b] book.title : xxxxxx 2008-11-30 19:42:46.532 SQLiteBooks[28013:20b] MasterViewController#tableView cellForRowAtIndexPath 2008-11-30 19:42:46.533 SQLiteBooks[28013:20b] book.title : ssssssss 2008-11-30 19:42:46.533 SQLiteBooks[28013:20b] MasterViewController#tableView cellForRowAtIndexPath 2008-11-30 19:42:46.533 SQLiteBooks[28013:20b] book.title : The Divine Comedy 2008-11-30 19:42:46.534 SQLiteBooks[28013:20b] MasterViewController#tableView cellForRowAtIndexPath 2008-11-30 19:42:46.534 SQLiteBooks[28013:20b] book.title : Mac OS X Internals 2008-11-30 19:42:46.534 SQLiteBooks[28013:20b] MasterViewController#tableView cellForRowAtIndexPath 2008-11-30 19:42:46.535 SQLiteBooks[28013:20b] book.title : War and Peace
しかし、AddViewController#saveのアクションがどこに設定されているのかイマイチわからない。
このプロジェクトには3つのxibファイル、MainWindow.xib、DetailView.xib、EditingView.xibがあるが
どのファイルを見ても下記のようなNew Book画面のリソースが見当たらないので
もう少し、ソースを追ってみることにする。