A newer version of this documentation is available.

View Latest

Databases

      +

      Description — Working with Couchbase Lite Databases
      Related Content — Blobs | Documents | Indexing

      Create or Open Database

      You can create a new database and-or open an existing database, using the CBLDatabase class. Just pass in a database name and optionally a CBLDatabaseConfiguration — see Example 1.

      Things to watch for include:

      • If the named database does not exist in the specified, or default, location then a new one is created

      • The database is created in a default location unless you specify a directory for it — see: CBLDatabaseConfiguration and CBLDatabaseConfiguration.directory()

        Best Practice is to always specify the path to the database explicitly.

        Typically, the default location for Objective-C is the application sandbox .

      Example 1. Open or create a database
      NSError *error;
      CBLDatabase *database = [[CBLDatabase alloc] initWithName:@"my-database" error:&error];
      if (!database) {
          NSLog(@"Cannot open the database:%@", error);
      }
      self.database = database;

      Close Database

      You are advised to incorporate the closing of all open databases into your application workflow.

      Closing a database is simple, just use CBLDatabase.Close() — see: Example 2. This also closes [1] active replications, listeners and-or live queries connected to the database.

      Closing a database soon after starting a replication involving it can cause an exception as the asynchronous replicator (start) may not yet be connected.
      Safely Closing a Database pre 2.8
      Before closing, check that any attached listeners (query/replication/change) indicate they are at least at connected status before closing — see for example: Monitor Status.
      Example 2. Close a Database
      if (![self.database close:&error])
          NSLog(@"Error closing db:%@", error);

      Database Encryption

      This is an Enterprise Edition feature.

      Couchbase Lite on Objective-C includes the ability to encrypt Couchbase Lite databases. This allows mobile applications to secure the data at rest, when it is being stored on the device. The algorithm used to encrypt the database is 256-bit AES.

      Enabling

      To enable encryption, use CBLDatabaseConfiguration.encryptionKey() to set the encryption key of your choice. Provide this encryption key every time the database is opened — see Example 3.

      Example 3. Configure Database Encryption
      CBLDatabaseConfiguration *config = [[CBLDatabaseConfiguration alloc] init];
      config.encryptionKey = [[CBLEncryptionKey alloc] initWithPassword:@"secretpassword"];
      
      NSError *error;
      self.database = [[CBLDatabase alloc] initWithName:@"my-database" config:config error:&error];
      if (!self.database) {
          NSLog(@"Cannot open the database:%@", error);
      }

      Persisting

      Couchbase Lite does not persist the key. It is the application’s responsibility to manage the key and store it in a platform specific secure store such as Apple’s Keychain or Android’s Keystore.

      Opening

      An encrypted database can only be opened with the same language SDK that was used to encrypt it in the first place. So a database encrypted using the Objective-C SDK, and then exported, is readable only by the Objective-C SDK.

      Changing

      To change an existing encryption key, open the database using its existing encryption-key and use CBLDatabase.changeEncryptionKey() to set the required new encryption-key value.

      Removing

      To remove encryption, open the database using its existing encryption-key and use CBLDatabase.changeEncryptionKey() with a null value as the encryption key.

      Upgrading

      To upgrade an encrypted database see: Upgrade 1.x databases

      Finding a Database File

      When the application is running on the iOS simulator, you can locate the application’s sandbox directory using the SimPholders utility.

      Database Maintenance

      From time to time it may be necessary to perform certain maintenance activities on your database, for example to compact the database file, removing unused documents and blobs no longer referenced by any documents.

      Couchbase Lite’s API provides the CBLDatabase.performMaintenance() method. The available maintenance operations, including compact are as shown in the enum CBLMaintenanceType to accomplish this.

      This is a resource intensive operation and is not performed automatically. It should be run on-demand using the API. If in doubt, consult Couchbase support.

      Command Line Tool

      cblite is a command-line tool for inspecting and querying Couchbase Lite databases.

      You can download and build it from the couchbaselabs GitHub repository. Note that the cblite tool is not supported by the Couchbase Support Policy.

      Troubleshooting

      You should use console logs as your first source of diagnostic information. If the information in the default logging level is insufficient you can focus it on database errors and generate more verbose messages — see: Example 4.

      For more on using Couchbase logs — see: Using Logs.

      Example 4. Increase Level of Database Log Messages
      CBLDatabase.log.console.domains = kCBLLogDomainAll;

      1. Commencing with Release 2.8