Maximizing Efficiency with NoSQL: Couchbase Lite and Realm for Mobile App Data Management

Oz Nusem
3 min readMar 22, 2015

--

Persistent data storage on mobile applications can be achieved through various methods such as shared preferences, user defaults, and writing and reading from files and databases. The appropriate option depends on the volume of data, intended use, and data type. SQLite3 is often the preferred database option for Android and iOS due to its integration with these platforms. There are also numerous wrapper libraries available to facilitate CRUD operations with SQLite3. However, this article will discuss alternative options originally developed to address big data challenges for servers, such as scalability and high write load. These options, known as NoSQL databases, may also offer benefits for mobile applications.

NoSQL Databases

Leading internet companies introduced NoSQL technology to address the limitations of traditional relational databases for modern web applications. Enterprises are now adopting it for various use cases driven by four interrelated trends: Big Users, Big Data, the Internet of Things, and Cloud Computing.

So why consider NoSQL for mobile app development?

One key advantage is the ability to update models without the need for migrations due to the schema-less nature of NoSQL. Additionally, NoSQL allows for the storage of model relationships through denormalization, enabling the retrieval of related data in a single document rather than through multiple queries.

Comparing NoSQL Options for Android: Couchbase Lite vs Realm

There are two leading NoSQL options for Android: Couchbase Lite and Realm. Couchbase Lite is a lightweight version of the popular Couchbase database, featuring the ability to create predefined queries known as views. It also offers the option to sync with a Couchbase server, which can be useful depending on the data used in the app. Realm is an open-source database that replaces SQLite and Core Data on iOS. It is easy to use and supports object relationships out of the box. Both options have pros and cons, and the best choice will depend on the app's specific needs.

Here are some examples (The code snippets are in Android code, but there is iOS equivalent syntax)

Creating the Couchbase lite db :

Saving a document :

Fetching a document :

Some Advantages and Disadvantages of Couchbase Lite.

Advantages:

  • Server synchronization capabilities
  • A strong reputation as a server database
  • Support for revisions
  • Schema-less design
  • Availability of views

Disadvantages:

  • Lack of model mapper in Android (only available in iOS)
  • No built-in support for object relationships
  • Requires manual mapping of POJOs for Android users.”

Using Realm for Mobile Data Storage

Realm is a popular open-source database solution designed for mobile devices, intending to replace SQLite and Core Data on iOS. It is easy to use and offers a range of useful features, including support for object relationships out of the box and the ability to work with any POJO without needing an object mapper. To store an object with Realm, make your POJO extend RealmObject and implement the required getters and setters.

Here’s an example of how to do this in Android:

Then we need to save it to our Realm:

One of the main advantages of Realm is its support for object relationships. To create a relationship, such as an address object, extend the class and include a RealmList member. This will allow you to manage relationships within your data easily

Pros:

  • Very easy to get started with. No initialization code required
  • It has a built-in object mapper based on reflections so that any POJO will work
  • Object relationships are supported out of the box and easy to set up
  • It can be easily integrated with network REST libraries such as Retrofit and Volley

Cons:

  • Does not have a straightforward way to handle migrations
  • Not schema-less, meaning it loses some of the benefits of NoSQL databases
  • Production readiness, Realms have been used in apps since 2012 but have not yet released a 1.0 version

In conclusion, persistent data storage is an important aspect of mobile app development, and various methods are available to achieve it. SQLite3 is a widely used database option, but NoSQL databases such as Couchbase Lite and Realm can also offer benefits for mobile applications. NoSQL databases are particularly useful for handling large volumes of data and can allow for more flexible data modeling and relationships.

Couchbase Lite and Realm have unique features, and it is important to carefully consider which option is the best fit for your specific app. Ultimately, the choice will depend on the volume and type of data, intended use, and specific requirements of the app.

Happy querying!

Some useful links:

http://developer.couchbase.com/mobile/

http://realm.io/

http://realm.io/docs/java/0.80.0/#realm-browser

https://github.com/couchbase/couchbase-lite-ios

--

--

Oz Nusem

Expert in mobile and wearable device connectivity development. Experienced software engineer and engineering manager."