Query returns nothing until realm is closed and reopened


#1

I’m trying to open a remote realm, make a query and then close it. The query results are empty until I close and reopen the realm. Can someone tell me what’s going on?

This is on ios 10. realm 1.13, realm object server 1.8.3, react-native 0.46.

Here’s my methods:

openRealm = () => {
    console.log('openRealm()')
    Realm.open({
          schema: [User, RankTitle, Interest, LocationHistory, PlanHistory],
          sync: {
            user: Realm.Sync.User.current,
            url: 'realm://..../dora'
          }
        }).then((realm) => {
          console.log('openRealm success with realm ' + JSON.stringify(realm));
          this.setState({realm: realm});
        }).catch((error) => {
          console.log('openRealm failure with error ' + error);
          this.setState({realm: null});
        });
  }
  closeRealm = () => {
    console.log('closeRealm()');
    this.state.realm.close();
  }

  readRealm = () => {
    console.log('readRealm()');
    let hists = this.state.realm.objects('PlanHistory');
    hists.addListener((name, changes) => {
      console.log('listener on hist... w/name ' + JSON.stringify(name) + ' changes' + JSON.stringify(changes));
      console.log(JSON.stringify(hists));
    });
    console.log(JSON.stringify(hists));
  }

And the log output:

Running application AwesomeProject ({
    initialProps =     {
    };
    rootTag = 1;
})
infoLog.js:17 Running application "AwesomeProject" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
App.js:35 componentWillMount()
App.js:40 registerwithProvider promise w/user {"identity":".....}
App.js:45 registerwithprovider done
App.js:57 openRealm()
App.js:65 openRealm success with realm {}
App.js:57 openRealm()
App.js:65 openRealm success with realm {}
App.js:81 readRealm()
App.js:87 {}
App.js:81 readRealm()
App.js:87 {}
App.js:76 closeRealm()
App.js:81 readRealm()
ExceptionsManager.js:65 Cannot access realm that has been closed.
handleException @ ExceptionsManager.js:65
handleError @ InitializeCore.js:106
reportFatalError @ error-guard.js:46
__guard @ MessageQueue.js:271
MessageQueue.callFunctionReturnFlushedQueue @ MessageQueue.js:107
(anonymous) @ debuggerWorker.js:72
ExceptionsManager.js:73 Unhandled JS Exception: Cannot access realm that has been closed.
reactConsoleErrorHandler @ ExceptionsManager.js:73
console.error @ YellowBox.js:69
logToConsole @ RCTLog.js:48
logIfNoNativeHook @ RCTLog.js:31
__callFunction @ MessageQueue.js:306
(anonymous) @ MessageQueue.js:108
__guard @ MessageQueue.js:269
MessageQueue.callFunctionReturnFlushedQueue @ MessageQueue.js:107
(anonymous) @ debuggerWorker.js:72
App.js:57 openRealm()
App.js:65 openRealm success with realm {}
App.js:81 readRealm()
App.js:87 {"0":{"id":"......}}

#2

This only happens when remote debugging is on, aka viewing the logs through a browser with react native’s debug tool.

Another issue I’ve encountered that only occurs with RN’s remote debugging on is that Realm.Sync.User.registerWithProvider() promise fails to resolve/reject.


#3

I suggest that you upgrade to Realm Js 2.1.0 (we have fixed and reworked many areas in 2.0.0).