Thursday, April 11, 2019

Local Database in React Native Application

In this post we are going to learn how to work with Local Database in React Native Application. More and more mobile applications need data to work, and databases have for quite a while been the most common way of storing and managing data. So, in some scenario, a mobile application uses a database that is hosted in the cloud, and connects remotely to it in order to access its data. This of course implies that, in order to be responsive, a mobile application needs an active and quite fast network connection. As a Mobile Application developer, we always search for a Local Database. A database which offers offline synchronization, reliable performance, security, and better integration with other stacks.

Local Database in React Native Application

Why use a mobile app database?
There are some advantages associated with using a mobile database:
  1. full offline modes for apps that depend on stored data.
  2. frugal on bandwidth for apps that depend on stored data.
  3. stable and predictable performance independent from network availability.
  4. personal data can be stored with the user, where some say they belong.
  5. Lightweight as storage is limited on mobile devices.
  6. No server requirement.
  7. In a form of library with no or very limited dependency (embeddable) so that it can be used when needed.
  8. Fast and secure.
  9. Easy to handle through code, and option to make it private or shared with other applications.
  10. Low memory and power consumption.

Most Popular Databases for Mobile Apps

There are lots of mobile databases coming into the market but not all of them satisfy all requirements mentioned  in this article. Let us discuss few of the most popular databases for mobile apps and try to highlight their characteristics and feature.

1. Realm 

Nowadays we have a lot of options when deciding to choose a database solution. We have different attractive options each of which has it's strengths and weaknesses. One I have found to be outstanding is Realm database. Realm database can be used by Android, IOS, React and even Xamarin developers.

When Realm was launched in 2014, the goal was to help mobile developers build better apps faster by giving them a robust alternative to SQLite and Core Data. The Realm Mobile Database is a cross-platform database solution that can be used as an alternative to SQLite and Core Data. Compared to these two options, Realm is easier to set up and use. To perform the same operation in Realm, you usually end up writing fewer lines of code than you would with SQLite or Core Data. On performance, Realm is said to be faster and it also offers other modern features such as encryption, JSON support and data change notifications.

Advantages of Realm over SQLite:
  1. easy to use
  2. faster than SQLite (up to 10x speed up over raw SQLite for normal operations)
  3. object conversion handled for you
  4. very responsive team
  5. Cross-platform support.
  6. convenient for creating and storing data on the fly.

2. Firebase

Firebase Realtime database is a cloud hosted database that supports multiple platforms Android, iOS and Web. All the data is stored in JSON format and any changes in data, reflects immediately by performing a sync across all the platforms & devices. This allows us to build more flexible realtime apps easily with minimal effort.Store and sync data with our NoSQL cloud database. Data is synced across all clients in realtime and remains available when your app goes offline.

The Firebase Real-time Database is a cloud-hosted database. Data is stored as JSON and synchronized in real-time to every connected client. When you build cross-platform apps with our iOS, Android, and JavaScript SDKs, all of your clients share one Real-time Database instance and automatically receive updates with the newest data.

3. ORMLite

ORMLite is lighter version of Object Relational Mapping which provide some simple functionality for persisting java objects to SQL databases. It is ORM wrapper over any mobile SQL related DB.

ORMLite is used to simplify complicated SQL operations by providing flexible query builder. It also provides powerful abstract Database Access Object (DAO) classes.

ORMLite is helpful in big size applications with complex queries because it handles "compiled" SQL statements for repetitive query tasks. It also has support for configuring of tables and fields without annotations and supports native calls to Android SQLite database APIs.

But ORMLite does not fulfil all the requirements, like it is bulky as compared to SQLite or Realm, slower than SQLite and Realm but faster than most of the other ORMs present in market.

All in all ORMLite is a good SQLite replacement if application is big and complex in terms of DB usage.

4. Berkeley DB

Berkeley DB is an open source high performance embedded DB that allow us to handle data in different ways. It was developed by Sleepycat Software but acquired by Oracle in 2006. It provides API for so many languages including Android and iOS.

Berkeley DB can handle data in many ways. It can be in relational way like SQLite (by replacing SQLite with its own library), or it can be in Key/Value pair data as byte arrays and supports multiple data items for a single key. It also supports java objects as data or it can also be XML documents. Different libraries provides different types of API to handle multiple formats but all packaged Berkeley DB.

Berkeley can work as relational DB as well as NoSQL DB (Depends on which library you are using). Good thing about Berkeley DB is that the API provided by it are compatible with SQLite. So one can use Berkeley DB without rewriting whole code again. Combination of Berkeley and SQLite is considered faster and perform better in concurrent and single writing multiple reading operations.

Berkeley is relatively faster than SQLite but because of so many different features it is bulkier than any other discussed DBs. So if Size of the application is a criteria try to use some other DB, unless you want a feature exclusively provided by Berkeley DB.

5. SQLite

SQLite is relational DB, a lighter version of SQL designed for mobile. It is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. It is an embedded SQL Database engine without any separate server process, unlike any other SQL database.

SQLite supports all the relational databases features and is open source compact library which is by default present in two main Mobile OS i.e. Android and iOS, and supported by blackberry and Windows phone.

SQLite can be stored both on disk as well as in memory and each database file is a single disk file and can be used cross-platform. It is very fast and need very less memory to operate.

6. PouchDB

PouchDB is an open-source JavaScript database inspired by Apache CouchDB which is designed to run well within the browser.If you want to use the Local Database in you React Native app then you should know the different factors about them so that you can decide which database is best suitable for your application.

Factors while selecting a database for your app:
  1. Offline-first.
  2. Database security.
  3. Read/Write Speed.
  4. Industry regulations.
  5. Supported data types.
  6. Pricing.
  7. Real-time sync.
This is all about Local Database in React Native Application. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.

1 comment:

  1. Much the same as other database programs, SQLite likewise makes a database document. Since it's this link an open-source program, it doesn't utilize a particular record augmentation.