29

I am about to undertake the development of a React Native app and am thoroughly convinced of the benefits of managing the app's state using Redux, however I'd like to make the app's data available whilst offline by using Realm for persistent storage. What I'm wondering is how Redux will play with Realm?

The app I'm developing will pull a large amount of JSON data via a RESTful API and then I'd like to persist this data to local storage - Realm seems to be an excellent option for this. What I'm unsure of however is how the Realm database will exist within the Redux store? Will it have to exist external to the store? Is using Realm within a Redux based app somehow a contradiction?

I've had a good search for articles describing the use of Realm, or other storage options (Asyncstorage or SQLite) for large datasets with Redux and could find little information.

oldo.nicho
  • 2,149
  • 2
  • 25
  • 39
  • 3
    Why not define LOAD/SAVE actions that actually make async calls to the realm database to load/save the right pieces of data. Sort of like treating th realm database like another service, because you can't load very large amounts of data in one shot. – iamnat Oct 21 '16 at 05:39
  • Might I ask, how large is "large"? – JeremyKun Feb 08 '17 at 00:53
  • hey, i'm having a similar issue: i extract and display data from Firebase realtime database into react native application for both iOS and android. My database is large (it contains more 14400 line) and it's mandatory for me to have all this data displayed even if there is no internet connection. So, i was thinking of making a jSON file that i read and write on data whenever there is no connection, but , then i found Realm. So do you think it meets with my needs ? i'm also using Redux in my app, will it have contradiction in my app ? Thank you – user3521011 May 02 '17 at 14:55
  • [This redux-persist issue](https://github.com/rt2zz/redux-persist/issues/83) discusses this particular subject. I don't think it will happen – DerpyNerd May 19 '17 at 09:29

1 Answers1

13

The redux store is good when you have only react components dealing with the data. The store is a good way to maintain your application's state. For example, you do not need Realm to store the current login status or flags indicating whether the user has skipped login. The redux store wins the game here.

On the other hand, Realm is the best when you have to deal with complex queries or a large amount of data to be stored. The advantage of having Realm is that the data can be accessed within your react components as well as non-react components/classes easily. Realm gives you the advantage to monitor your data with the Realm Browser and build relationships between your models. Realm also wins the race if you have to do any offline sync.

Will it have to exist external to the store - Yes.

Is using Realm within a Redux based app somehow a contradiction - It depends upon what you are using the storage for.

Jaseem Abbas
  • 5,028
  • 6
  • 48
  • 73
  • Thank you for your excellent response :-) Since writing this question I have a much better understanding on what redux is and how it is used and I totally agree with your statements above. I am still not completely sure whether I need to use Realm or whether a Redux store is going to be adequate. I suspect the later for my use case. Thanks again. – oldo.nicho Feb 08 '17 at 14:41
  • We are making a similar decision - did you make a final decision, and if so, are you happy with the choice? We have implemented Realm somewhat already - but are finding it to add a lot of overhead to our project and are considering removing it. I just wonder how much offline matters for many mobile apps - especially as ours is ecom and not something people ever expect to do offline. – Mary Camacho Mar 29 '17 at 15:15
  • I am the Co-Founder of clockit.io. Here at clockit we store users geo location in the background as a part of offline location tracking. Redux vs Realm totally depends upon your application's requirement. If you are using ecom, using realm will cause an overhead unless you have very specific offline storage requirements. – Jaseem Abbas Mar 29 '17 at 16:34
  • 3
    In my humble opinion, Redux and Realm shoudn't be exclusive to each other. It's only a matter of sorting out different layers of one's application. React and Redux is mainly on the presentation layer where a Redux store(even though it's named a store) is not and should not be a persistence device of any sort. Redux store simply stores different variables for UI components to behave accordingly. On the other hand, Realm solves problems at persistence layer. One should fetch data and display them using React and Redux, and cache them offline using Realm according to one's business logic. – CarlLee Sep 23 '17 at 09:56
  • 1
    does Realm itself offer some of the benefits of Redux then? that is, does it offer it's own way to automatically update UI components if the data changes? – Greg Sep 24 '17 at 08:57
  • @Greg Realm offers notifications that can be used to trigger UI refreshes, though you'll need to check if the granularity matches the needs of your application. I can imagine a data flow where user interaction triggers network requests and the response writes changes to Realm which triggers a Realm notification that dispatches a Redux action with the latest Realm data. https://realm.io/docs/javascript/latest/#notifications – blwinters Jul 11 '20 at 15:18