react-nativeandroid-emulatorrealmandroid-device-monitor

Bundle pre-populated realm with react native app


I am attempting to bundle a pre-populated realm with a react native app.

I have followed this, in an attempt to get this working for android. https://github.com/realm/realm-js/issues/532

Which advised to put the realm in the assets folder:(android/app/src/main/assets). I have done so:

enter image description here

However, when I run Realm.copyBundledRealmFiles() in the contrructor of my base component like so:

constructor(props) {
    super(props);
    Realm.copyBundledRealmFiles();
  }

I get the build error:

enter image description here

Any help in fixing this so that I can bundle pre-populated realms with an app would be extremely helpful. I have been working on this for quite a few hours now and just can't seem to figure it out!


Solution

  • Ok figured this out for android:

    1) In the android/app/src/main/assets folder paste the realm.

    2) in index.android.js: Use copyBundledRealmFiles() to move them over:

    import { AppRegistry } from 'react-native';
    import React from 'react';
    import Realm from 'realm';
    import App from './src/App';
    
    const App2 = () => {
      Realm.copyBundledRealmFiles();
      return (
        <App />
      );
    };
    
    AppRegistry.registerComponent('SenecaV2', () => App2);
    

    then close all terminals and run react-native run-android to rebundle.

    You can check if the realm is being compied over by using the command line to go to (on mac)

    library/android/sdk/tools

    then the command monitor

    this will open up the device monitor. Then, in the device monitor click on file mangager and navgigate to data/data/{packageName}/files and you should see the realm copied over.

    If you then want to be able to reference that path in your app use the package:

    https://github.com/johanneslumpe/react-native-fs

    To give you the path to the files system:

    const path = `${RNFS.DocumentDirectoryPath}/{nameOfYourPrePackagedRealmFile}`
    

    You can then use this in the new Realm constructors config path property to access the realm.

    https://realm.io/docs/javascript/1.0.0/api/Realm.html#path

    Hope this helps anyone with the same problem!