realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

addListener notification is empty

Open allenw23 opened this issue 7 years ago • 27 comments

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();
        });

allenw23 avatar Mar 18 '17 17:03 allenw23

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.

kristiandupont avatar Mar 20 '17 10:03 kristiandupont

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 avatar May 07 '17 18:05 bennybennet

@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 avatar May 09 '17 09:05 kristiandupont

@kristiandupont

  1. I do get a notification for the first insertion with a changeset of 0 insertions.
  2. 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
        });
      }
    });

bennybennet avatar May 10 '17 18:05 bennybennet

Well in that case, there is a bug. Is it possible for you to share some code that reproduces the behavior?

kristiandupont avatar May 11 '17 09:05 kristiandupont

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.

tybro0103 avatar May 13 '17 19:05 tybro0103

Yep, exactly what @tybro0103 is saying. React Native, Realm 1.3.0, no special code.

bennybennet avatar May 17 '17 11:05 bennybennet

thanks, added this as bug. we will be looking into it

blagoev avatar May 17 '17 11:05 blagoev

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.

lamflam avatar Jun 24 '17 22:06 lamflam

Same problem here.

stefanocdn avatar Jun 26 '17 20:06 stefanocdn

I've just encountered this as well, it's one change behind every single time.

jmparsons avatar Jul 05 '17 21:07 jmparsons

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.

kasperkronborg avatar Jul 18 '17 09:07 kasperkronborg

I'm experiencing the same bug with latest realm and RN 0.46.x. It's very critical.

sheshavpd avatar Jul 27 '17 16:07 sheshavpd

@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 avatar Jul 27 '17 17:07 lamflam

@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.

sheshavpd avatar Jul 28 '17 06:07 sheshavpd

+1

jjdp avatar Aug 01 '17 10:08 jjdp

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 avatar Sep 15 '17 09:09 sheshavpd

@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 avatar Sep 17 '17 05:09 elfman

@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.

sheshavpd avatar Sep 20 '17 06:09 sheshavpd

Realm: 1.12.0 RN: 0.47 Platform: iOS 11 BUT only in simulator - on iPhone SE no problem

kosiakMD avatar Sep 27 '17 13:09 kosiakMD

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.

brooksyott avatar Oct 15 '17 14:10 brooksyott

@kosiakMD I'm having the same issue, WITH Realm: 1.13.0

fee-studio avatar Oct 24 '17 08:10 fee-studio

+1

niros001 avatar Nov 29 '17 15:11 niros001

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?

xmedeko avatar Dec 17 '19 08:12 xmedeko

Still an issue in "2.29.1", and the double-overhead "workaround" makes me depressed.

AlexanderPinkerton avatar Dec 30 '19 22:12 AlexanderPinkerton

@AlexanderPinkerton. What happens if you upgrade to the latest version of Realm JS (3.6.0)?

kraenhansen avatar Jan 07 '20 08:01 kraenhansen

➤ 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.

sync-by-unito[bot] avatar Jul 04 '22 12:07 sync-by-unito[bot]