question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use/package a Realm file in React Native ?

See original GitHub issue

I’ve got a realm file “cleverbean.realm” that I want to reference in React Native. My first thought was I could just place the file (“cleverbean.realm”) into its own directory in the React Native project, and then reference that when instantiating the realm.

I copied a realm file into a path off root. ie

./data/cleverbean.realm

image

I then tried the following to instantiate :

realm = new Realm({
   path: './data/cleverbean.realm',
   schema: [CleverbeanSchema]
});

That didnt work and threw this error thinking I was trying to resolve via a non project path. image

How should I go about ensuring that a realm will be pre-installed on iOS and Android, in the context of a React Native app ? Also what is the correct way to reference those local Realms, when instantiating in the app (per the example code snippet above) ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14

github_iconTop GitHub Comments

13reactions
twinzencommented, May 2, 2017

Thanks ajonno for your detail explanation. Not sure if we are on the same topic… But actually, I was finding a way to put my pre-populated DB data file into my app, and finally I found a solution!

I post my solution, as I believe people may find this thread when they want to do the same thing - include populated DB data file into app.

However, as I only need a solution for iOS, my solution would cover iOS only. For Android, I think it can be done in similar way.

  1. Put your realm DB file into your XCode project. screen shot 2017-05-02 at 10 24 00 pm

  2. Be reminded to add it under Copy Bundle Resource otherwise the file will not be copied to you app when build. You can find it under Build Phases screen shot 2017-05-02 at 10 23 34 pm

  3. Define realm DB file path when you declare realm object (This is the most tricky thing… I spent a night to find out solution, because I am newbie of iOS and react-native) The key point is that You need to give the full path of the realm DB file that you put in XCode project. To get the full path, I use react-native-fs to achieve this. With RNFS.MainBundlePath, you can have your app’s base path, and your realm DB file should be there. Another tricky thing is that, you must declare your realm as readOnly: true (Maybe it is not a must if you include some more files to your XCode project, but I didn’t try it) screen shot 2017-05-02 at 10 30 00 pm

Hope above solution can help people that encountered same problem.

6reactions
varun-rajcommented, Oct 24, 2017

@vladbars Will this copy the realm file every time we open the app?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bundle a Realm File - React Native SDK - MongoDB
Open up the android folder generated by React Native in Android Studio. · In the Project tree, navigate to app > src >...
Read more >
How to use/package a Realm file in React Native? - YouTube
How to import Realm file in React Native project.
Read more >
Where does realm React-Native store data on mobile apps?
Realm stores data in a proprietary binary format in a .realm file on the devices filesystem. If you are running the iOS simulator...
Read more >
How to find realm file — React Native | by Gokulanath A S
How to find realm file — React Native. console.log ("Simulator path of realm file", realm.defaultPath);.
Read more >
Top 11 Local Databases for React Native App Development
It makes use of an SDF data file. In case of React Native, there are multiple databases, some of which are – Realm,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found