couchbase-lite-ios
Lightweight, embedded, syncable NoSQL database engine for iOS and MacOS apps.
Top Related Projects
Lightweight, embedded, syncable NoSQL database engine for Android.
Realm is a mobile database: a replacement for Core Data & SQLite
A type-safe, Swift-language layer over SQLite3.
A toolkit for SQLite databases, with a focus on application development
A Cocoa / Objective-C wrapper around SQLite
YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers.
Quick Overview
Couchbase Lite for iOS is a lightweight, embedded NoSQL database for mobile and edge devices. It provides offline-first capabilities, synchronization with Couchbase Server, and supports iOS, macOS, and tvOS platforms. The library is designed to be efficient, reliable, and easy to integrate into iOS applications.
Pros
- Offline-first architecture allows apps to work without constant internet connection
- Synchronization capabilities with Couchbase Server for seamless data sharing
- Supports multiple platforms (iOS, macOS, tvOS) with a single codebase
- Efficient and lightweight, suitable for mobile and edge devices
Cons
- Learning curve for developers new to NoSQL databases
- Limited query capabilities compared to traditional SQL databases
- Requires additional setup and configuration for synchronization features
- May not be suitable for applications with complex relational data models
Code Examples
- Creating a database and document:
import CouchbaseLiteSwift
do {
let database = try Database(name: "mydb")
let document = MutableDocument()
.setString("John Doe", forKey: "name")
.setInt(30, forKey: "age")
try database.saveDocument(document)
} catch {
print("Error: \(error)")
}
- Querying documents:
let query = QueryBuilder
.select(SelectResult.all())
.from(DataSource.database(database))
.where(Expression.property("age").greaterThan(Expression.int(25)))
do {
let results = try query.execute()
for result in results {
if let dict = result.toDict() {
print("Document: \(dict)")
}
}
} catch {
print("Error executing query: \(error)")
}
- Setting up replication:
let url = URL(string: "ws://localhost:4984/mydb")!
let target = URLEndpoint(url: url)
let config = ReplicatorConfiguration(database: database, target: target)
config.replicatorType = .pushAndPull
config.continuous = true
let replicator = Replicator(config: config)
replicator.start()
Getting Started
-
Install Couchbase Lite for iOS using CocoaPods. Add to your Podfile:
pod 'CouchbaseLite-Swift'
-
Run
pod install
in your project directory. -
Import the library in your Swift file:
import CouchbaseLiteSwift
-
Initialize a database:
do { let database = try Database(name: "mydb") // Use the database } catch { print("Error initializing database: \(error)") }
-
Start using Couchbase Lite features like creating documents, querying, and replication.
Competitor Comparisons
Lightweight, embedded, syncable NoSQL database engine for Android.
Pros of couchbase-lite-android
- Better integration with Android-specific features and APIs
- More extensive documentation for Android developers
- Larger community of Android developers contributing to the project
Cons of couchbase-lite-android
- Slightly slower performance in some scenarios compared to iOS counterpart
- Less frequent updates and releases
- Some advanced features may be implemented later than in the iOS version
Code Comparison
couchbase-lite-android:
Database database = new Database("mydb");
Document document = new MutableDocument()
.setString("type", "user")
.setString("name", "John Doe");
database.save(document);
couchbase-lite-ios:
let database = try Database(name: "mydb")
let document = MutableDocument()
.setString("type", "user")
.setString("name", "John Doe")
try database.saveDocument(document)
Both repositories provide similar functionality for their respective platforms. The main differences lie in the language-specific syntax and platform-specific optimizations. The Android version uses Java, while the iOS version uses Swift. Both implementations follow similar patterns for database operations, document creation, and data manipulation.
The choice between these repositories depends on the target platform and the developer's familiarity with the respective ecosystems. Both offer robust solutions for mobile database management with Couchbase Lite.
Realm is a mobile database: a replacement for Core Data & SQLite
Pros of Realm Swift
- Simpler API and easier to use, especially for beginners
- Faster performance for most operations
- Better integration with Swift language features
Cons of Realm Swift
- Less flexible querying capabilities
- Limited support for custom data types
- Smaller community and ecosystem compared to Couchbase
Code Comparison
Realm Swift:
let realm = try! Realm()
let dog = Dog()
dog.name = "Rex"
try! realm.write {
realm.add(dog)
}
Couchbase Lite iOS:
let database = try Database(name: "mydb")
let doc = MutableDocument()
doc.setString("Rex", forKey: "name")
try database.saveDocument(doc)
Both libraries provide local database solutions for iOS apps, but they differ in their approach and features. Realm Swift offers a more straightforward API and better performance for most use cases, making it easier for developers to get started. However, Couchbase Lite iOS provides more advanced querying capabilities and better support for custom data types, which can be crucial for complex applications.
The code comparison shows that Realm Swift has a more concise syntax for basic operations, while Couchbase Lite iOS requires a bit more setup. However, this simplicity in Realm Swift can sometimes come at the cost of flexibility in more advanced scenarios.
A type-safe, Swift-language layer over SQLite3.
Pros of SQLite.swift
- Lightweight and focused solely on SQLite, making it more suitable for simple database needs
- Type-safe query building, reducing runtime errors and improving code readability
- Extensive documentation and examples, making it easier for developers to get started
Cons of SQLite.swift
- Lacks synchronization capabilities, which Couchbase Lite iOS provides for multi-device scenarios
- No built-in support for complex data types or JSON documents, unlike Couchbase Lite's flexible document model
- Limited querying capabilities compared to Couchbase Lite's N1QL-like query language
Code Comparison
SQLite.swift:
let db = try Connection("path/to/db.sqlite3")
let users = Table("users")
let id = Expression<Int64>("id")
let name = Expression<String>("name")
try db.run(users.create { t in t.column(id, primaryKey: true); t.column(name) })
Couchbase Lite iOS:
let database = try Database(name: "mydb")
let document = MutableDocument()
document.setString("John Doe", forKey: "name")
document.setInt(30, forKey: "age")
try database.saveDocument(document)
Both libraries offer easy-to-use APIs for database operations, but Couchbase Lite iOS provides a more flexible document-based approach, while SQLite.swift focuses on traditional relational database operations with type-safe query building.
A toolkit for SQLite databases, with a focus on application development
Pros of GRDB.swift
- Lightweight and focused on SQLite, offering better performance for local database operations
- More flexible query API with support for raw SQL and Swift query interface
- Easier integration with existing SQLite databases and migration from other SQLite-based solutions
Cons of GRDB.swift
- Lacks built-in synchronization capabilities, which Couchbase Lite iOS provides out-of-the-box
- Limited support for complex data types and document-oriented storage compared to Couchbase Lite
- Smaller community and ecosystem compared to Couchbase's broader platform
Code Comparison
GRDB.swift query example:
let players = try Player.filter(Column("score") > 1000).order(Column("name")).fetchAll(db)
Couchbase Lite iOS query example:
let query = QueryBuilder
.select(SelectResult.all())
.from(DataSource.collection("players"))
.where(Expression.property("score").greaterThan(Expression.int(1000)))
.orderBy(Ordering.property("name"))
let results = try query.execute()
Both examples demonstrate querying a database, but GRDB.swift offers a more concise syntax for SQLite operations, while Couchbase Lite iOS provides a more verbose but flexible API for document-oriented queries.
A Cocoa / Objective-C wrapper around SQLite
Pros of FMDB
- Lightweight and simple to use, focusing solely on SQLite operations
- Extensive documentation and community support
- Easy integration into existing projects without additional dependencies
Cons of FMDB
- Lacks built-in synchronization capabilities
- No out-of-the-box support for complex data types or JSON documents
- Limited querying capabilities compared to Couchbase Lite's N1QL support
Code Comparison
FMDB example:
let db = FMDatabase(path: "myDatabase.sqlite")
db.open()
db.executeUpdate("INSERT INTO users (name, age) VALUES (?, ?)", withArgumentsIn: ["John", 30])
let results = db.executeQuery("SELECT * FROM users WHERE age > ?", withArgumentsIn: [25])
db.close()
Couchbase Lite example:
let database = try Database(name: "myDatabase")
let document = MutableDocument()
.setString("John", forKey: "name")
.setInt(30, forKey: "age")
try database.saveDocument(document)
let query = QueryBuilder
.select(SelectResult.all())
.from(DataSource.database(database))
.where(Expression.property("age").greaterThan(Expression.int(25)))
let results = try query.execute()
Both FMDB and Couchbase Lite iOS offer database solutions for iOS applications, but they cater to different use cases. FMDB is a simpler, SQLite-focused library, while Couchbase Lite provides a more feature-rich, NoSQL-oriented solution with synchronization capabilities.
YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers.
Pros of YapDatabase
- Lightweight and focused on iOS/macOS, potentially offering better performance for Apple platforms
- Simpler setup and integration for iOS developers familiar with Objective-C and Swift
- Supports advanced features like full-text search and secondary indexes out of the box
Cons of YapDatabase
- Limited cross-platform support compared to Couchbase Lite's broader ecosystem
- Lacks built-in synchronization capabilities for multi-device scenarios
- Smaller community and fewer resources available for support and troubleshooting
Code Comparison
YapDatabase:
let database = YapDatabase(path: databasePath)
let connection = database.newConnection()
connection.readWrite { transaction in
transaction.setObject(myObject, forKey: "key", inCollection: "collection")
}
Couchbase Lite:
let database = try Database(name: "mydb")
let document = MutableDocument()
.setString("John Doe", forKey: "name")
.setInt(30, forKey: "age")
try database.save(document)
Both libraries offer straightforward APIs for basic database operations, but Couchbase Lite's API is more document-oriented, while YapDatabase uses a key-value approach with collections.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Couchbase Lite for iOS and MacOS
Couchbase Lite is an embedded lightweight, document-oriented (NoSQL), syncable database engine.
Couchbase Lite implementation is on top of Couchbase Lite Core, which is also a new cross-platform implementation of database CRUD and query features, as well as document versioning.
Requirements
- iOS 12.0+ | macOS 12.0+
Installation
Swift Package
Requirements:
- XCode 12+
Community Edition
dependencies: [
.package(name: "CouchbaseLiteSwift",
url: "https://github.com/couchbase/couchbase-lite-ios.git",
from: "3.2.0-beta.1"),
],
Enterprise Edition
dependencies: [
.package(name: "CouchbaseLiteSwift",
url: "https://github.com/couchbase/couchbase-lite-swift-ee.git",
from: "3.2.0-beta.1"),
],
More detailed information on how to setup is available here: swift package manager
CocoaPods
You can use CocoaPods to install CouchbaseLite
for Objective-C API or CouchbaseLiteSwift
for Swift API by adding it in your Podfile:
Objective-C
Community Edition
target '<your target name>' do
use_frameworks!
pod 'CouchbaseLite'
end
Enterprise Edition
target '<your target name>' do
use_frameworks!
pod 'CouchbaseLite-Enterprise'
end
Swift
Community Edition
target '<your target name>' do
use_frameworks!
pod 'CouchbaseLite-Swift'
end
Enterprise Edition
target '<your target name>' do
use_frameworks!
pod 'CouchbaseLite-Swift-Enterprise'
end
Carthage
You can use Carthage to install CouchbaseLite
by adding it in your Cartfile:
Community Edition
binary "https://packages.couchbase.com/releases/couchbase-lite-ios/carthage/CouchbaseLite-Community.json"
Enterprise Edition
binary "https://packages.couchbase.com/releases/couchbase-lite-ios/carthage/CouchbaseLite-Enterprise.json"
When running
carthage update or build
, Carthage will build both CouchbaseLite and CouchbaseLiteSwift framework.
How to build the framework files.
- Clone the repo and update submodules
$ git clone https://github.com/couchbase/couchbase-lite-ios.git
$ cd couchbase-lite-ios
$ git submodule update --init --recursive
-
If not already installed, install doxygen,
brew install doxygen
-
Run ./Scripts/build_framework.sh to build a platform framework which could be either an Objective-C or a Swift framework. The supported platforms include iOS, tvOS, and macOS.
$ ./Scripts/build_framework.sh -s "CBL ObjC" -p iOS -o output // For building the ObjC framework for iOS
$ ./Scripts/build_framework.sh -s "CBL Swift" -p iOS -o output // For building the Swift framework for iOS
Documentation
Sample Apps
- Todo : Objective-C and Swift
License
Like all Couchbase source code, this is released under the Apache 2 license.
Top Related Projects
Lightweight, embedded, syncable NoSQL database engine for Android.
Realm is a mobile database: a replacement for Core Data & SQLite
A type-safe, Swift-language layer over SQLite3.
A toolkit for SQLite databases, with a focus on application development
A Cocoa / Objective-C wrapper around SQLite
YapDB is a collection/key/value store with a plugin architecture. It's built atop sqlite, for Swift & objective-c developers.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot