プログラムの変更
チュートリアルの完了後、自分のモバイルアプリを作る際には iOS リファレンスガイド を参照しながら作業を進めます。ここでは、その練習として簡単な修正を加えてみます。
Hello Kii では一覧内の項目をタップするとその項目が削除されます。この機能を、MyObject n
の部分を HH:mm:ss 形式の現在時刻に更新する機能に変更してみます。
一覧内の項目がタップされたときの処理は MainViewController.swift
または MainViewController.m
の tableView(_:didSelectRowAt:)
メソッドに記述されています。一旦メソッド内のコード全体を削除しておきます。
-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { }
-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }
コードのコピー
KiiObject の更新方法にはいくつかありますが、今回は単純な上書き方法である、更新チェックなしのフルアップデートを実装します。更新機能の実装について詳しくは、iOS リファレンスガイドの フルアップデート(更新チェックなし) を参照してください。
リファレンスガイドの各 API のコード例には、ブロッキング API とノンブロッキング API の両方が記載されています。すでに見たように、作業スレッドを自分で管理しない場合はノンブロッキング API を使用します。
まずは、リファレンスガイドの必要箇所をコピー&ペーストでソースファイルに貼り付けます。以下の画面は Swift のサンプルからコピーする例ですが、Objective-C にもサンプルコードが用意されています。
-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // Update key-value pairs. object.setObject(NSNumber(value: 1 as Int), forKey: "myid") object.setObject("John Doe Jr", forKey: "name") object.setObject("john_jr@example.com", forKey: "email") object.remove(forKey: "address") // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. object.saveAllFields(true, with: { (object : KiiObject?, error : Error?) -> Void in if error != nil { // Handle the error. return } }) }
-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Update key-value pairs. [object setObject:[NSNumber numberWithInt:1] forKey:@"myid"]; [object setObject:@"John Doe Jr" forKey:@"name"]; [object setObject:@"john_jr@example.com" forKey:@"email"]; // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. [object saveAllFields:YES withBlock:^(KiiObject *object, NSError *error) { if (error != nil) { // Handle the error. return; } }]; }
内容の調整
次に、処理の前後を調整します。
- サンプルコードの
object
は更新したい KiiObject です。タップ位置の KiiObject はobjectList
から取得できます。 - サンプルコードの
setObject(_:forKey:)
メソッドはダミーの値を書き込みます。これを実際に書き込むデータに修正します。書き込むデータは iOS のDateFormatter
クラスで作成できます。
ここまでで、次のようなコードになります。
-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let object = self.objectList[indexPath.row] // Set the key-value pair. let df = DateFormatter() df.dateFormat = "HH:mm:ss" let value = df.string(from: Date()) object.setObject(value, forKey: objectKey) // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. object.saveAllFields(true, with: { (object : KiiObject?, error : Error?) -> Void in if error != nil { // Handle the error. return } }) }
-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { KiiObject *object = _objectList[indexPath.row]; // Set the key-value pair. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"HH:mm:ss"; NSString* value = [dateFormatter stringFromDate:[NSDate date]]; [object setObject:value forKey:OBJECT_KEY]; // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. [object saveAllFields:YES withBlock:^(KiiObject *object, NSError *error) { if (error != nil) { // Handle the error. return; } }]; }
さらに、完了後の処理を追加します。
通常、リファレンスガイドのサンプルコードは、コールバックメソッド内で行う完了時の処理が空になっています。今回追加する処理では、失敗時には画面にメッセージを表示し、成功時には画面に変更を反映します。
これらを追加すると、最終的に以下のようなコードになります。
-
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let object = self.objectList[indexPath.row] // Set the key-value pair. let df = DateFormatter() df.dateFormat = "HH:mm:ss" let value = df.stringFromDate(NSDate()) object.setObject(value, forKey: objectKey) // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. object.saveAllFields(true, with: { (object : KiiObject?, error : Error?) -> Void in if error != nil { self.showMessage("Update failed", error: error as NSError?) return } self.objectList[indexPath.row] = object! self.tableView.reloadData() }) }
-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { KiiObject *object = _objectList[indexPath.row]; // Set the key-value pair. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"HH:mm:ss"; NSString* value = [dateFormatter stringFromDate:[NSDate date]]; [object setObject:value forKey:OBJECT_KEY]; // Save and fully update the KiiObject. // This method removes all key-value pairs from the KiiObject on the server and // adds the key-value pairs generated locally to the KiiObject. [object saveAllFields:YES withBlock:^(KiiObject *object, NSError *error) { if (error != nil) { [self showMessage:@"Update failed" error: error]; return; } [self.objectList replaceObjectAtIndex:indexPath.row withObject:object]; [self.tableView reloadData]; }]; }
実行
実際にビルドして実行すれば、現在時刻に更新できる様子を確認できるはずです。
このように、リファレンスガイドのサンプルコードをコピーして、前後の処理を調整すれば、目的の機能を簡単に実現できます。今回呼び出すメソッドは saveAllFields(_:with:)
、クロージャーの引数の型は KiiObject
と Error
ですが、これらの組み合わせもサンプルコードをコピーすれば容易に実装できます。
なお、API の仕様そのものは、Objective-C の形式で appledoc に記載されています。iOS リファレンスガイドとあわせてご覧ください。
次は...
チュートリアルの最後に、Kii Cloud を理解するためのヒントとなる情報や、実際のモバイルアプリを作成するときに知っておくとよい機能を紹介します。
次のステップへのヒント に移動してください。