Realm-js: "TypeError: snapshot method was not called on a Realm object!" when running with RN debugger

Created on 26 Apr 2018  路  30Comments  路  Source: realm/realm-js

Goals

Create a frozen result collection from a query result in react-native while debugging.

Expected Results

Expect this to work the same way it does when remote debugging is not running.

Actual Results

An exception is thrown: "TypeError: snapshot method was not called on a Realm object!"

Note that this same query works well when the debugger isn't running.

This is what the object looks like in the debugger.

screen shot 2018-04-26 at 11 17 25

Steps to Reproduce

Hope to be able to provide those soon.

Code Sample

Hope to be able to provide those soon.

Version of Realm and Tooling

  • Realm JS SDK Version: 2.2.20, also 2.4.0.
  • Node or React Native: RN 0.55.3
  • Client OS & Version: macOS 10.13.4 (17E199), simulating iOS 11.3
  • Which debugger for React Native: regular builtin, using Chrome
O-Community Pipeline-Discussion T-Bug

Most helpful comment

@kneth In which way is it possible, that dispatching managed objects leads to a crash? You seem to imply that's an OK behavior for them.

I take that from you saying "dispatching [managed objects] seems to [be] the problem".

"The problem" is listener results missing certain methods, isn't it? A bug which hasn't been addressed in a year.

All 30 comments

Same problem on 2.4.0

@febeling Thanks for reporting. Just a quick question: are filtered() and sorted() working in debug mode?

@kneth I get the same problem with sorted() and filtered() results here.

The results are in fact coming from a result listener. Not sure if that adds relevant information.

@febeling Thanks for the update. Combining your two observations hint that we should investigate the listener code.

Hello, any progress on this issue? It's getting really hard to proceed without a debugger

Sorry, no. We have had other recent priorities.

@kneth Is there anything I can do to help with this?

@febeling @Sovent No, but if methods like sorted() and snapshot() ain't working in the debugger, it sounds like the RPC is not triggered in all cases as it should.

For us this prevents using the debugger anywhere in app development, which is quite a burden. As there has been no progress since late April and apparently no priority on solving this, we now consider replacing realm with an alternative.

Same problem here. Any news on this issue?

@kneth I'm running into this without trying to use the browser REPL. that is, if the the debugger isn't attached my code here works:

realmListener = (collection: Collection<Match>, changes: CollectionChangeSet) => {
    try{
        const matches = collection.snapshot().slice().map(toPlainMatch);
        console.log("real listener called", matches);
        this.setState({
            matches
        })
    }catch(e){
        console.log("Error", e, collection);
    }
}

componentDidMount() {
    openRealm().then((realm: Realm): void => {
        this.realmStore.addListener(this.realmListener);
    })
}

and when remote debugging is enabled I receive the error

Error TypeError: snapshot method was not called on a Realm object!
    at Proxy.<anonymous> (util.js:43)
    at Proxy.MatchesComponent._this2.realmListener (component.tsx:130)
    at sendRequest (rpc.js:290)
    at rpc.js:307
    at JSTimers.js:256
    at _callTimer (JSTimers.js:152)
    at Object.callTimers (JSTimers.js:405)
    at MessageQueue.__callFunction (MessageQueue.js:349)
    at MessageQueue.js:106
    at MessageQueue.__guard (MessageQueue.js:297) Proxy聽{Symbol(realm): undefined, Symbol(id): 14,聽鈥

@hcliff It might be that Collection doesn't have snapshot() and only Results has.

@kneth I got to the bottom of this - another thread was closing the db. the error message wasn't very helpful but really PEBCAK

thanks!

@hcliff Happy to hear that you got it fixed. I agree that the error message didn't help you. If you can create a very short example to reproduce the situation and error message, we might be able to understand it better - and provide a better error message in the future.

I also have this issue, But I cant think of a thread that does close on the database
because I didn't implement anywhere a close function.

@kneth Seems the problem was

this.store.dispatch({type: "UPDATE_CONVERSATIONS", data: [...entities]})

entities is coming from collection.addListener callback
doing this instead solved my problem

let entitiesCopy = entities.map((item) => {return item});
this.store.dispatch({type: "UPDATE_CONVERSATIONS", data: entitiesCopy});

@xcxooxl Thanks. And an interesting solution. entities are managed objects, and dispatching them seems to the problem. Maybe https://www.npmjs.com/package/realm-react-redux can be useful.

@kneth In which way is it possible, that dispatching managed objects leads to a crash? You seem to imply that's an OK behavior for them.

I take that from you saying "dispatching [managed objects] seems to [be] the problem".

"The problem" is listener results missing certain methods, isn't it? A bug which hasn't been addressed in a year.

I have the same issue 馃様

I have a listener over the results.
Inside the listener, if I call .snapshot over the collection... It leads to a crash in the "debug" mode.

It works well in "release" mode, but in "debug" mode it crashes the app with Error: "results.snapshot was called on non-Realm object"

Here is the code sample:

const currentFoldersList = FoldersDB
    .objects()
    .filtered(`parentFolderId = "${currentFolderId}" && name CONTAINS[c] "${searchText}"`)
    .sorted('name');

currentFoldersList.addListener(this.updateFolders);

this.unsubscribeFolders = () => {
    currentFoldersList.removeListener(this.updateFolders);
};

Where

updateFolders = (collection) => {
    this.setState({
        folders: collection.snapshot()
    }); 
}

As workaround I did a hack:

updateFolders = (collection) => {
    this.setState({
        folders: __DEV__ ? collection : collection.snapshot()
    }); 
}

You may ask why do I need a snapshot() of the results...

Here is more interesting thig...
The results works very well in debug mode and triggers rerender.
But in release mode it can not rerender component by setState.
Event forceUpdate does not impact! that's crazy :0

@kneth is it any way to run Realm in release mode while I'm actualy in debug mode?

I found a lot of inconsistencies when I test the app first time in release mode!
Many things which worked in debug - does not work in release anymore and reverse! :)

No way to using in release mode when debugging. I believe that might actually lead to many more inconsistencies (we have to keep the Realm in the debugger and the device in sync).

The "results.snapshot was called on non-Realm object" error does sound like a bug and we need to investigate it.

@kneth would be nice :) May I help somehow?)

@chelovekdrakon If you can create a small, self-contained example which can reproduce it, it would be a very good start.

@febeling I could confirm this error. Very frustrating experience. Javascript and React Native support always was second-class citizen.

And this is a very critical bug.

Even though according to https://blog.bitrise.io/state-of-app-development-in-2019 React Native has 14% of mobile market share. And definitely will grow.

@likern as Kenneth said, if you can create a small, self-contained example which can reproduce the issue, we'd be happy to take a look.

@TuTejsy Could you explain why you insert your issue number in every other issue you can find ?

@TuTejsy Could you explain why you insert your issue number in every other issue you can find ?

I think my issue has the same case and connected with mutating objects in realm. The difference that i use one object and here used collection. I inserted it here because think, that its possible to reproduce it in my environment

We have reworked a large part of the Chrome debugger support, and I suggest an upgrade to v10.1.4.

If the issue is still observed, please create a new issue.

Was this page helpful?
0 / 5 - 0 ratings