realm-js
realm-js copied to clipboard
addListener notification is empty
When using addListener(objects, changes) on React Native iOS it always returns empty on the first notification and the second notification returns what should have been in the first one.
I found in results-tests.js below that first notification is empty.
Is this by design or am I doing something wrong here? :grinning:
realm.objects('TestObject').addListener((testObjects, changes) => {
// TODO: First notification is empty, so perform these
// assertions on the second call. However, there is a race condition
// in React Native, so find a way to do this in a robust way.
//TestCase.assertEqual(testObjects.length, 4);
//TestCase.assertEqual(changes.insertions.length, 1);
resolve();
});
Yes, this can be a bit surprising. You can think of it as an async query -- the first notification gives you the "initial" result. We should document this better.
If I get this right, the listener get triggered for the current change but the parameter changes
have the changes from the last change, right? That doesn't feel right to me. I would like to have the current changes in an EventListener, not the one before the current.
What is the recommended solution to get the current changes when the event is triggered?
@bennybennet no, it's just that you will always get an initial, empty notification. After that, you will be notified with each change and the changeset represents that specific change.
@kristiandupont
- I do get a notification for the first insertion with a changeset of 0 insertions.
- When i do another insertion i do get a notification with a changeset of 1 insertion, but that one is the change from 1.
What i did now as a workaround is
const txs = realm.objects('Transaction');
let currentLength = txs.length;
realm.addListener('change', (tx, changes, x, y, z) => {
if(currentLength < txs.length) {
currentLength = txs.length;
let newTransactions = this.state.transactions;
newTransactions.unshift(this.buildTxObject(txs[txs.length - 1]));
this.setState({
transactions: newTransactions
});
}
});
Well in that case, there is a bug. Is it possible for you to share some code that reproduces the behavior?
I'm experiencing the same thing in React Native. Realm v1.2.0
. No special setup or code.
But another important note here... The initial invocation of the callback (the one that shows no changes), doesn't happen until the first change is triggered. Basically every single invocation receives the args from what should have been the previous invocation. Seems like they're getting queued up, but the queue has an off-by-1 bug.
Just upgraded to 1.3.0
. Same thing.
Yep, exactly what @tybro0103 is saying. React Native, Realm 1.3.0, no special code.
thanks, added this as bug. we will be looking into it
Also having this issue in React Native with Realm 1.3.1. After banging my head for a few hours trying to figure out what I was doing wrong I finally realized that these notifications are just always one realm.write()
behind. I eventually just added and additional realm.write(() => {})
call which makes it trigger the notification I need right away. Definitely seems to work as a workaround, but looking forward to having a real fix for this.
Same problem here.
I've just encountered this as well, it's one change behind every single time.
I'm getting the same behaviour with Realm 1.8.1 and React Native 0.44.3. No solution to this yet? I'm willing to upgrade versions to get this to work.
I'm experiencing the same bug with latest realm and RN 0.46.x. It's very critical.
@sheshavpd This was a critical issue for me as well, and for now I got it working by doing realm.write(() => {})
(basically a second empty write) after each write. Far from ideal and may not be feasible in your context, but it's been working well for me in the meantime.
@lamflam yes, I'm using the same workaround. It "works" for me too. But as per my understanding, the workaround might take one extra roundtrip time to database since it starts and commits a transaction. Thanks.
+1
This issue has disappeared for me in the version 1.11.1
, for now. I never used chrome debugger, though.
I finally switched back to adb logcat
command to get console logs. RN Apps that use Realm can never use chrome debugging due to huge performance issues. Someone please confirm if this issue still exist for you.
@sheshavpd I still get the same problem in my app.
Realm: 1.11.1
RN: 0.47.1
Platform: iOS 10.2 in simulator && Android 6.0
@elfman, Sorry for not specifying my environment.
Realm: 1.11.1
RN: 0.48.3
Platform: Working well on all Android physical devices with version > Android 4.x.
Realm: 1.12.0 RN: 0.47 Platform: iOS 11 BUT only in simulator - on iPhone SE no problem
I'm having the same issue, with version 1.6.0 of the C# library. If I trigger a refresh, I get the right change set.
@kosiakMD I'm having the same issue, WITH Realm: 1.13.0
+1
The issue still exists in realm-js 3.5.0.
If you cannot fix it, what about to have some option for the Realm.open()
, so that realm-js will issue one more empty write after each write automatically?
Still an issue in "2.29.1", and the double-overhead "workaround" makes me depressed.
@AlexanderPinkerton. What happens if you upgrade to the latest version of Realm JS (3.6.0)?
➤ Stoney Burks commented:
We discussed in team meeting that this issue specifically will be handled through documentation. We will, however, create a new feature/enhancement ticket to address long-term solutions.