7

I have played around with Realm, got it to work with React-Native - Got the data inserted from JSON file and was able to filter/read it too.

Now the real question, how can i pre-package the data with my react-native app. I have seen some questions, where it is mentioned on how to do it with Native (IOS) and (Android)

I wanted to check if there is a direct way to do it with React-Native, may be a bridge someone created?

Thanks!

Community
  • 1
  • 1
Abhay
  • 401
  • 3
  • 12

2 Answers2

2

Ideally you would be able to just use the path of your bundled realm file when opening the Realm. Unfortunately this will not work until support for read-only realms is added. I created an issue to track this: https://github.com/realm/realm-js/issues/392

Until read-only realms are supported, you can copy the bundled realm file into your Documents directory at app launch in your AppDelegate and then use that path to access the bundled Realm. Relative paths are supported so if you copy your file to <Documents>/bundled.realm you can open this file by just passing in the file name, ie new Realm({path: 'bundled.realm'})

Ari
  • 1,439
  • 10
  • 7
  • Hi @Ari - So i tried the above approach where i bundled a pre-loaded realm file. It works well. I also wrote the Objective C code to add the default.realm file to the Documents. Now, the problem is, when i try to make any update. I get an exception from React-Native. Please note, I had to add the React.Framework for IOS in my xcode to import the `import ` - This is the culprit, when i remove it, everything works. Please let me know if you have any direction or need more details. – Abhay Apr 25 '16 at 19:24
  • @Abhay what exception are you seeing? If the schema you are using in React Native is at all different from your Objective C schema then you would get an exception during Realm initialization. If you don't pass a schema parameter when opening the Realm in JS, it should just use the schema used from Objective C which could avoid this problem, although this has not bee thoroughly tested. – Ari Apr 26 '16 at 20:19
  • Thanks for the response. I am using the same default.realm file that was generated to schemaVersion: 1... So there is no change in the file. The file gets copied... and i am also able to read the data.. I am getting an issue, where it's failing to write to the database. Here is a gist of what i have in my AppDelegate.m - https://gist.github.com/abhaytalreja/69e16e8a2384123a4257483fa55fb14c – Abhay Apr 26 '16 at 20:27
  • (This could be a different issue) On a parallel note, i am also trying to create data using an Async process - https://github.com/corbt/next-frame and after the save of the first item, i am getting an error. The error says `"http://localhost:8082/commit_transaction net::ERR_CONTENT_LENGTH_MISMATCH"` - That's all i see in the browser, I don't know Objective C, but here is what the console says `"libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument"` – Abhay Apr 26 '16 at 20:30
  • When you are removing a Realm file it isn't enough to just delete the path to the `.realm` file. There is also a lock file which needs to be removed, and log files which should also be removed. These files live next to the Realm file in the file system. Your issue may be due to a lock file being left over. There was a request to add a method to make it easier to remove all of these files which should make it into the next release. Until then you can also try calling `Realm.clearTestState();` which closes all open realms and deletes all Realm files in the documents directory. – Ari Apr 26 '16 at 20:40
  • Thanks for quick response. Do i need to do Realm.clearTestState in javascript? or through xcode? Also, when i did all this, i manually deleted all the files present before a fresh installation. So on startup, it should copy the files. and while connecting to Realm, it creates the rest of the files. – Abhay Apr 26 '16 at 20:50
  • I even tried setting the property of readOnly - No, cause i think everything works, it's only while writing there is a problem. `configuration.readOnly = NO;` - But still no luck. – Abhay Apr 27 '16 at 15:30
  • 1 more piece of udpate, i tired to create a new record v/s the record i was updating, seems like even create doesn't work. i wonder what i am missing. – Abhay Apr 27 '16 at 15:37
  • @Abhay not sure stackoverflow is the best place to continue this discussion. If you can share your project on github or if you send it to help@realm.io I would be happy to take a look. – Ari Apr 28 '16 at 18:23
  • As the new version of the package is released, this can be done using the RNFS - file systems library. – Abhay Jul 01 '16 at 19:22
1

we have created a utility method to access

  • assets folder from android.
  • resources from iOS.

export function readOnetContent() {
    let mainbundelPath = RNFS.MainBundlePath + "/oNetContent.realm"
    if (Platform.OS == "android") {
        Realm.copyBundledRealmFiles();
        mainbundelPath = "oNetContent.realm"
    }

    return new Promise((resolve, rejects) => {
        Realm.open({
             schema: Schema,
             path: mainbundelPath,
             readOnly: true})
            .then(realm => {
                let oNetContent = realm.objects(OnetContentSchema.name)
                resolve(oNetContent)
            })
            .catch(error => {
                console.log("error: ", error)
                rejects(error)
            })
    })
}
  • directory structure for IOS is :

enter image description here

  • directory structure for Android is :

enter image description here

Tushar Pandey
  • 4,557
  • 4
  • 33
  • 50