Realm-js: There is a issue if use both realm-java and realm-js

Created on 30 May 2016  路  10Comments  路  Source: realm/realm-js

In my android app, I use both realm-java and realm-js. After I created a object in reaml-java, then I query it in react-native with realm-js, but I get null.

This is my code:

// create a Baby object
Realm realm = Realm.getDefaultInstance();

Baby baby = new Baby();
baby.name = "Baby Name";

realm.beginTransaction();
realm.copyToRealm(baby);
realm.commitTransaction();
// then query in react-native
let baby = realm.objects('Baby').filtered('name = "Baby Name"')[0];
// but the result of baby is undefined

It won't get the result until I reload react-native JS. But I debug in chrome with stetho_realm, I can see the object has been created.

I don't know whether it's a bug, or maybe there is a error in my code.
But that's OK in my ios app with using both realm-swift and realm-js.

My environment are:

Mac OS 10.11
Android 5.1
node 5.3
realm-js 0.11.1
java version "1.8.0_66"
io.realm:realm-gradle-plugin:0.88.2

T-Bug

Most helpful comment

FWIW, I found the following workaround to be effective:

  1. Add a java-side event that's fired into the javascript side when the realm changes (via io.realm.Realm.addChangeListener or io.realm.DynamicRealm.addChangeListener).
  2. Debounce change events on the javascript side and do an empty write() on the realm for it to update.

All 10 comments

@wusuopu Thanks so much for taking the time to report this issue. I'm working on fixing this bug right now, and it should work in the next release.

As a workaround for right now, I suggest sending a message to the JS when changes have been made, and in the JS, you should close (realm.close()) the Realm and re-open it (realm = new Realm(config)). That should allow it to access the latest data.

@appden Your advice is working for me. But I have find another way to avoid this issue.
When changes have been made in realm-java, I also execute a write operation in realm-js. In this way, I can allow it to access the latest data.

I'm still getting this issue. I created a new React Native project and installed realm via npm (it installed version 1.12.0 of the realm npm module). On the Android side of things I'm using version 3.7.2 via the realm gradle plugin. I can add an object on the Android side of things but I only see an updated object count in RN if I use @wusuopu's method of executing an empty write. The problem with that is that the count is alway one behind the actual count. I see this appears to be fixed so am wondering is there something I'm missing in order for my change listener to see changes made via Java?

Happens to me as well. I am on Realm-java 4.2.0 and Realm-JS 2.0.7

I was hoping after upgrading to the latest on both sides the issue would be fixed (similar to @gouravd). After reading over the changes in #469, it's not immediately apparent how those changes address the problem.

This issue is still persistent in latest Build 4.3.1

FWIW, I found the following workaround to be effective:

  1. Add a java-side event that's fired into the javascript side when the realm changes (via io.realm.Realm.addChangeListener or io.realm.DynamicRealm.addChangeListener).
  2. Debounce change events on the javascript side and do an empty write() on the realm for it to update.

Hi, I want to ask a similar question here. How can I use both RealmSwift and RealmJS in an iOS app? When I try to integrate both RealmSwift and RealmJS, I run into duplicate symbol. How can I solve this?

@danleechina while that is similar, I would recommend you make a new issue. That sounds like an entirely different problem.

For everyone still facing the issue, the key thing to make it work is the empty write. The Realm.close() and reopen didn't worked for me.
I am sending the event through a broadcast (from native to react) after I write stuff on the Android side, and doing the empty write on react native

Was this page helpful?
0 / 5 - 0 ratings

Related issues

texas697 picture texas697  路  3Comments

matt2legit picture matt2legit  路  3Comments

jmartindivmedianet picture jmartindivmedianet  路  3Comments

fever324 picture fever324  路  4Comments

kevinnguy picture kevinnguy  路  3Comments