days
-1
-4
hours
-1
-3
minutes
-2
-9
seconds
-4
-3
search
Make it so

Can you use Realm as the local DB in app development with React Native?

Purvak Pathak
Realm
© Shutterstock / Scanrail1

React Native has excellent performance for small teams thanks to its architecture, native offloading, databases, and more. In this article, Purvak Pathak describes benefits of Realm as a local database in React Native app development.

React Native is awesome, no doubt there. It highly performant and can do wonders for small teams, providing them with the agility of an enterprise. It has an exceptional performance that could only be challenged by Flutter when we look into the cross-platform solutions.

The performance of React Native apps depends upon several factors: Architecture, Native offloading, and Databases to name a few. In this article, we are going to walk you through some really great features of Realm database which makes it the number one choice among other local databases.

Let’s dive into them, one by one!

Zero-copy design architecture

Unlike traditional databases such as SQLite, Realm is based on zero-copy design architecture which avoids unnecessary copying of raw data by mapping whole data in a file. Realm does this by simply calculating the offset, reading data from memory mapped region and returning the raw value thereafter. This means that there’s no need of deserialization; skipping unnecessary workload making it exceptional faster than other relational databases.

Zero copy architecture in Realm

Zero copy architecture in Realm

The figure below represents benefits of a zero-copy in Realm. As demonstrated in the figure, whenever PersonProxy object is created in Realm, the Person object will be queried automatically with the new values. This instant modification of objects results in retrieving faster results.

More than just a wrapper

Most databases are built as the wrappers of common ORMs. For example, SQLite is a wrapper built around SQL database. However, Realm is more than just a wrapper. In fact, Realm offers you ample of features that most ORMs can’t even claim to afford.

One such feature is the ability to store data as an object graph store. This means every data stored in Realm is treated as an object which makes it above par, unlike other relational databases.
Moreover, whenever a wrapper is built around any technology stack, it creates a layer of abstraction layer around it. Realm binding libs are built very thin to cut out this abstraction complexity. This makes the components work better while syncing with each other.
Another benefit of eliminating abstraction complexity in Realm is the access to other referenced objects by following pointers. While querying relationships in Realm, you get raw links to the object file which makes data querying even easier than other relational databases.

Faster in performing queries

The Realm database persists object file directly from the disk which makes it capable of performing data queries, even more, faster than other local databases of React Native. Also, the read/write operations in Realm takes place on main thread without interruption.

An extensive study done on the performance comparison of Realm vs SQLite vs Coredata depicted Realm as the clear winner among the local databases.

Better in declaring relationships to the components

Realm as an object-oriented database provides a much better way of declaring relationships between its components.

One-to-one relationship 

public class Contact extends RealmObject { private Email email; // Other fields… }

Many-to-many relationship

While creating one or many relationships, you need to set the property type to iList<> and extending the class with the class in which we are storing the data

[public class Contact extends RealmObject { public String name; public iList emails; } public class Email extends RealmObject { public String address; public boolean active; }

Supports Multithreading

Realm supports multi-threading which means you can have as many numbers of threads working on the same component in parallel. Realm takes care of this by automatically refreshing the data on every thread in order to commit smoother transactions.
Moreover, Realm does not support the need of locking resources this way as all the threads are executing their own logic.
However, you can’t have multiple threads running on the same instance in Realm. That’s why you need to create a separate instance every time while doing multithreading. Rest will be taken care by Realm itself as data will be refreshed automatically after the commitment of the transaction.

Great support from community

While working with Realm, you will have the opportunity to expect a great community support almost everywhere. You will find the community members answering your queries on the important channels such as Hackernews, StackOverflow, Quora and Reddit.

Moreover, the Realm documentation has much more to offer you and we are sure you won’t be disappointed while trying it for your app.

When we were working with Realm for our app back in 2015, we were overwhelmed by the support and enthusiasm that this community gives us. Almost most of the issues we had have while configuring realm for our app were resolved with the help of community members.

Security

The Realm database also provides you with some security features such as data encryption for your React Native app. In fact, Realm team, in their official documentation related to security states that

“… Enabling encryption of a realm file is as simple as adding a single line of code to the realm configuration. iOS, macOS, tvOS and watchOS versions of Realm use the CommonCrypto library whereas the Windows uses the built-in Crypto library and Android platforms use OpenSSL.”

Realm uses standard AES-256 encryption to encrypt and decrypt data. Which means everytime you want to use the data stored in Realm, you need to encrypt first with a key. This is great security feature which will prevent your data from any sort of theft and misuse.

Conclusion

Listed above are some of the benefits you should know about Realm as the local database for your React Native app.

Though, SQLite alone is a powerful local database for your React Native app. However, it is not useful when your application needs to scale with the increasing number of users. Realm, as a local database is the best alternative to be used in such type of case.

So, are you ready to consider realm in your React Native app? Let us know in comments below!

Author

Purvak Pathak

React Native Evangelist. I travel the world, tell stories about React native, and help people implement it in their mobile solutions.


Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of