Database for Swift

    Working with Couchbase Lite Databases

    New Database

    As the top-level entity in the API, new databases can be created using the Database class by passing in a name, configuration, or both. The following example creates a database using the Database(name: String) method.

    do {
        self.database = try Database(name: "my-database")
    } catch {

    Just as before, the database will be created in a default location. Alternatively, the Database(name: Strings, config: DatabaseConfiguration?) initializer can be used to provide specific options in the DatabaseConfiguration object such as the database directory.

    Database Encryption

    This is an Enterprise Edition feature.

    Couchbase Lite on Swift 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.

    To enable encryption, you must set the DatabaseConfiguration.encryptionKey property to the encryption key of your choice. Provide this encryption key every time the database is opened.

    let config = DatabaseConfiguration()
    config.encryptionKey = EncryptionKey.password("secretpassword")
    self.database = try Database(name: "my-database", config: config)

    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.

    An encrypted database can only be opened with the same language SDK that was used to encrypt it in the first place (Swift, C#, Java, Java (Android) or Objective-C). For example, if a database is encrypted with the Swift SDK and then exported, it will only be readable with the Swift SDK.

    Upgrading from 1.x when Encryption is Enabled

    If you’re migrating an application from Couchbase Lite 1.x to 2.x, note that the automatic database upgrade functionality is not supported for encrypted databases. Thus, to upgrade an encrypted 1.x database, you should do the following:

    1. Disable encryption using the Couchbase Lite 1.x framework (see 1.x encryption guide)

    2. Open the database file with encryption enabled using the Couchbase Lite 2.x framework.

    Since it is not possible to package Couchbase Lite 1.x and Couchbase Lite 2.x in the same application this upgrade path would require two successive upgrades. If you are using Sync Gateway to synchronize the database content, it may be preferable to run a pull replication from a new 2.x database with encryption enabled and delete the 1.x local database.

    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.

    CLI tool

    cblite is a command-line tool for inspecting and querying Couchbase Lite 2.x 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.


    If you are using a Couchbase Lite release prior to 2.5 see Deprecated functionality

    Couchbase Lite provides a logging API that unifies the logging behavior across all platforms[1], making debugging and troubleshooting easier during development and in production.

    The retrieval of logs from the device is out of scope of this feature.

    Available logging features include:

    • Console based logging

    • File based logging

    • Replication logging

    • Custom logging

    Console based logging

    Default: Enabled.

    Console based logging is often used to facilitate troubleshooting during development.

    File based logging

    Default: Disabled.

    Available file based logging formats:

    • Binary — most efficient for storage and performance. It is the default for file based logging.

    • Plaintext

    We recommend using the binary log format and a decoder, such as cbl-log, to view them. Download cbl-log from couchbaselabs/couchbase-mobile-tools.

    The following example enables file based logging.

    Example 1. Enabling file logging
    let tempFolder = NSTemporaryDirectory().appending("cbllog")
    Database.log.file.config = LogFileConfiguration(directory: tempFolder)
    Database.log.file.level = .info

    Replication Logging

    The following example increases the log output for activity related to replication with Sync Gateway.

    Example 2. Setting replication log-level
    // Replicator
    Database.setLogLevel(.verbose, domain: .replicator)
    // Network
    Database.setLogLevel(.verbose, domain: .network)

    Custom logging

    Default: Disabled.

    Allows registration of a callback function to receive Couchbase Lite log messages, which may be logged using any external logging framework.

    Apps must implement the Logger interface — see Example 3 — a And set it on the custom property — see Example 4.

    Example 3. Implementing logger interface
    fileprivate class LogTestLogger: Logger {
        // set the log level
        var level: LogLevel = .none
        func log(level: LogLevel, domain: LogDomain, message: String) {
            // handle the message, for example piping it to
            // a third party framework
    Example 4. Enabling custom logging
    Database.log.custom = LogTestLogger()

    Decoding binary logs

    You can use the cbl-log tool to decode binary log files — see Example 5.

    Example 5. Using the cbl-log tool
    • macOS

    • CentOS

    • Windows

    Download the cbl-log tool using wget.


    Navigate to the bin directory and run the cbl-log executable.

    $ ./cbl-log logcat LOGFILE <OUTPUT_PATH>

    Download the cbl-log tool using wget.


    Navigate to the bin directory and run the cbl-log executable.

    cbl-log logcat LOGFILE <OUTPUT_PATH>

    Download the cbl-log tool using PowerShell.

    Invoke-WebRequest -OutFile

    Run the cbl-log executable.

    $ .\cbl-log.exe logcat LOGFILE <OUTPUT_PATH>

    Logging functionality prior to Release 2.5

    The pre-Couchbase Lite 2.5 logging functionality is deprecated. It was superseded by an enhanced Logging API in that release. This information is included for completeness only.

    The log messages are split into different domains (LogDomains) which can be tuned to different log levels. The following example enables verbose logging for the replicator and query domains.

    Example 6. Enable verbose logging on a domain
    Database.setLogLevel(.verbose, domain: .replicator)
    Database.setLogLevel(.verbose, domain: .query)
    Database.setLogLevel(.verbose, domain: .replicator)
    Database.setLogLevel(.verbose, domain: .query)

    1. From version 2.5