はじめに
本稿では、Objective-CによるiOSアプリ開発に、NoSQLドキュメント指向モバイルデータベース Couchbase Liteを使うための具体的な方法について解説します。
本稿では、Couchbase Lite 3.0.0ベータ版をObjective-Cアプリケーションで利用する際の動作確認までを解説します。
なお、Swiftによるアプリケーション開発に関する同趣旨の内容については、以下を参照ください。
また、Couchbaseをモバイルアプリケーションで利用する意義については、以下の記事をご参考ください。
確認環境
- macOS Catalina Version 10.15.7
- Xcode Version 12.4
- Carthage 0.38.0
実行手順
Carthageによるインストール
1.Cartfileを作成し、以下を追加します。
Community Editionの場合
binary "https://packages.couchbase.com/releases/couchbase-lite-ios/carthage/CouchbaseLite-Community.json" ~> 2.8
Enterprise Editionの場合
binary "https://packages.couchbase.com/releases/couchbase-lite-ios/carthage/CouchbaseLite-Enterprise.json" ~> 2.8
2.carthage update --platform ios
を実行します。
3.CouchbaseLite.frameworkをCarthage/Build/からXcodeナビゲーターにドラッグします。
4.[TARGETS]> [General]> [Framework, Libraries, and Embeded Content]セクションのCouchbaseLite.frameworkを確認します
ソースコード編集
今回は、稼働確認のため、ViewController.mのviewDidLoad
メソッドを編集します。
まず、下記のインポート文を追加する必要があります。
# import <CouchbaseLite/CouchbaseLite.h>
今回のコードでは、基本的なCRUD操作と単純なクエリの実行を行っています。ViewController.mの全体を示します。
//
// ViewController.m
// CBL_Objective-C
//
// Created by Yoshiyuki Kono on 2022/01/05.
//
# import "ViewController.h"
# import <CouchbaseLite/CouchbaseLite.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
bool *testReplication;
testReplication = false;
NSError *error;
CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"mydb" error:&error];
// Create a new document (i.e. a record) in the database.
CBLMutableDocument *mutableDoc = [[CBLMutableDocument alloc] init];
[mutableDoc setFloat:2.0 forKey:@"version"];
[mutableDoc setString:@"SDK" forKey:@"type"];
// Save it to the database.
[database saveDocument:mutableDoc error:&error];
// Update a document.
CBLMutableDocument *mutableDoc2 = [[database documentWithID:mutableDoc.id] toMutable];
[mutableDoc2 setString:@"Objective-C" forKey:@"language"];
[database saveDocument:mutableDoc2 error:&error];
CBLDocument *document = [database documentWithID:mutableDoc2.id];
// Log the document ID (generated by the database)
// and properties
NSLog(@"Document ID :: %@", document.id);
NSLog(@"Learning %@", [document stringForKey:@"language"]);
// Create a query to fetch documents of type SDK.
CBLQueryExpression *type = [[CBLQueryExpression property:@"type"] equalTo:[CBLQueryExpression string:@"SDK"]];
CBLQuery *query = [CBLQueryBuilder select:@[[CBLQuerySelectResult all]]
from:[CBLQueryDataSource database:database]
where:type];
// Run the query
CBLQueryResultSet *result = [query execute:&error];
NSLog(@"Number of rows :: %lu", (unsigned long)[[result allResults] count]);
}
@end
実行結果確認
ビルドして実行します。下記のようなログがコンソールに出力されます。
2022-01-05 11:49:29.599455+0900 CBL_Objective-C[27456:711931] CouchbaseLite/3.0.0 (ObjC; iOS 14.4; iPhone) Build/0 Commit/e4d1b957+CHANGES LiteCore/3.0.0 (302)
2022-01-05 11:49:29.599884+0900 CBL_Objective-C[27456:711931] CouchbaseLite Database WARNING: CBLDatabase.log.file.config is nil, meaning file logging is disabled. Log files required for product support are not being generated.
2022-01-05 11:49:29.632321+0900 CBL_Objective-C[27456:711931] Document ID :: -NfXPS7-z8dCoZ0WnP1gWLM
2022-01-05 11:49:29.632504+0900 CBL_Objective-C[27456:711931] Learning Objective-C
2022-01-05 11:49:29.634684+0900 CBL_Objective-C[27456:711931] Number of rows :: 1
最後に
本稿では、Objective-Cアプリケーション開発のために、プロジェクトにCouchbase Liteを追加し、基本的な操作が行えるところの確認までを行いました。
最後に、さらなるステップに進むための情報を示して、本稿の締め括りとしたいと思います。
Couchbase Liteについての記事を以下の投稿で整理していますので、ご関心に応じて、参照してみてください。
本稿の記事の内容(インストール、動作確認)に関する、一次情報として、下記のドキュメントがあります。
Couchbase Liteは、Couchbase Serverとの自動双方向同期が可能です。
本稿で扱っていない、Couchbase Serverについては、日本語で読むことができるまとまった情報として、次の拙著を紹介させていただきます。
関連情報
Carthageについて、下記の記事を参考にさせていただきました。