Ankit Tale
Ankit Tale

Reputation: 2004

How to Push Realm File to Internal Storage in React Native from Application

I am working on a application which ships whole database file shared within React Native Code to user internal storage. For pushing file to internal storage on Android I am using Realm the code we have done work correctly for iOS but for Android I am getting file not found (aka. The Realm is not pushing an file to Internal Storage Android)

Here what I done until now

    export default new Realm({
    path:Platform.OS === 'ios'
        ? RNFS.MainBundlePath +'/www/RippleEffect.realm'
        : RNFS.DocumentDirectoryPath +'/RippleEffect.realm',

    schema: [apply_it, 
        case_study,
        got_it, 
        got_it_2, 
        got_it_2_question, 
        got_it_2_question_answer, 
        got_it_question, 
        got_it_question_answer, 
        howto,
        howto_screens,
        info,
        info_screens,
        model,
        profile,
        profile_result,
        profile_statements,
        related_topic,
        special_assets_table_teens,
        topic_audio,
        topic_equivalence,
        topics,
        true_story,
        your_mind,
    ],
    readOnly: true
  });

Use this two dependency

import Realm from 'realm';
import RNFS from 'react-native-fs'

And In App Class

const App = () => {
  Realm.copyBundledRealmFiles();
  return (
   <App/>
  );

};

Upvotes: 1

Views: 843

Answers (1)

Ankit Tale
Ankit Tale

Reputation: 2004

For those who are working React Native with Realm DB

I solve this issue by pushing realm file from assets folder to internal folder(user mobile ) and thus I was able to read an write about database.

 File dbDirectory = new File(MainApplication.getFileDirectory() + "/db");

    /**
     * Check for database
     * directory
     */
    if(!dbDirectory.exists()){
      if(dbDirectory.mkdirs()){
        moveDBFileToCard("database.realm");
      }
    }

    private void moveDBFileToCard(String databaseFileName) {
    try{
      AssetManager assetsManagerFiles = getAssets();
      /**
       * Intialize Stream
       */
      InputStream inputStream;
      OutputStream outputStream;

      inputStream = assetsManagerFiles.open(databaseFileName);
      System.out.println("File Path = " + MainApplication.getFileDirectory() + "/db" + "/" + databaseFileName);
      File fileDB = new File(MainApplication.getFileDirectory() + "/db" + "/" + databaseFileName);
      if (fileDB.exists()) {
        fileDB.delete();
      }
      outputStream = new FileOutputStream(MainApplication.getFileDirectory() + "/db" + "/" + databaseFileName);
      byte[] buffer = new byte[1024];
      int read;

      while ((read = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, read);
      }

      inputStream.close();
      outputStream.flush();
      outputStream.close();
    }catch (IOException io){
      io.printStackTrace();
    }
  }

And also removed
readOnly: true from file as it was giving issue to filter quesry on 0.60 version

Upvotes: 2

Related Questions