[React Native] subscription is a blank object?


#1

I wrote a demo app following “Partially synced Realms” on the documentation. However it doesn’t work as I imaged.

Here is code:

  async openDatabase() {
    const user = await Realm.Sync.User.login(REALM_SERVER, USERNAME, PASSWORD);
    const config = {
      schema: [heroSchema],
      sync: {
        user,
        url: 'realm://192.168.1.185:9080/heroes',
        partial: true,
        error: err => console.error(err),
      }
    }
    const db = await Realm.open(config);
    return db;
  }
    this.db = await this.openDatabase();

    const results = this.db.objects('Hero').sorted('name');
    const subscription = results.subscribe();

    results.addListener((collection, changes) => {
      console.log('listener fired');
      console.log('subscription', subscription);
      switch(subscription.state) {
        case Realm.Sync.SubscriptionState.Error:
          console.error('Sync error');
          break;
        case Realm.Sync.SubscriptionState.Complete:
          const heroes = collection.slice();
          console.log(heroes);
          this.setState({ heroes });
          break;
        default:
          console.log('Sync statue', subscription.state);
      }
    })

subscription is always a blank object {}, so subscription.state is always undefined.

did I miss something?


#2

The documentation seems to have little type: results.addListener should be subscription.addListener. But I believe I have been able to reproduce a bug in RN Realm JS (at least on Android). It means that subscriptions aren’t working as expected. You can use a listener on results instead:

result.addListener((collection, changes) => {
    // use collection and/or changes
});

The listener will be called when partial sync completes. I have updated the documentation (will arrive at the website soon).

We will investigate why subscriptions are not working on React Native.


#3

This is the same as this: https://github.com/realm/realm-js/issues/1886