Accessing object of type * which has been invalidated or deleted.
See original GitHub issueHello everyone!
Goals
I am trying to get values from my realm and print them in a flatlist in react native
Expected Results
I would expect it to print my flatlist without any issues.
Actual Results
The error : Accessing object of type ListIncomes which has been invalidated or deleted. But when I check if my object is valid, (isValid()), I get true, I can even make a console.log with it.
Steps to Reproduce
First, I created a first Realm that I modified just to add an id. I also changed the name of ListIncomeSchemaOk and ListIncomes but it doesn’t change anything. Weirdest thing is that the text “TEST” is written on my app and is printed on my screen just before the error comes. It seems logical since I can make a console.log(Array.from(realm.objects(‘ListIncomes’))) and the right thing is printed.
Code Sample
The definition of my realm:
const ListIncomeSchemaOk = { name: 'ListIncomes', properties: { id: 'string', charterer: 'string', type: 'string', tonage: 'float', price: 'float', from: 'string', to: 'string', fromyear: 'int', frommonth: 'int', fromday: 'int', toyear: 'int', tomonth: 'int', today: 'int' } };
When I try to get my values:
componentDidMount(){ Realm.open({schema: [ListIncomeSchemaOk], schemaVersion: currentVersion}) .then(realm => { this.setState({listItems: Array.from(realm.objects('ListIncomes'))}) realm.close(); }) .catch(error => { console.log(error); }) }
And in the render:
<FlatList data={this.state.listItems} keyExtractor={(item) => item.id} renderItem={({item}) => <Text>TEST</Text>} />
Version of Realm and Tooling
- Realm JS SDK Version: 4.0.0-beta.0
- Node or React Native: react-native-cli: 2.0.1 and react-native: 0.61.5
- Client OS & Version: Windows 10 PRO 1909
- Which debugger for React Native: ?/None
I am pretty sure it is a stupid error but I checked others similar issues on github and stack but all add an issue with navigation (but I don’t have any navigation on my file) or with the validity but my object is valid… I really don’t understand and it’s making me crazy! Sorry if it is a stupid error Thanks in advance and have a nice evenning 😃
EDIT: Actually it was an issue with me closing realm too soon, so now I close it in the componentWillUnmount
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (1 by maintainers)
Yes, as I said in the edit I was closing realm too soon. now I only close it on componentWillUnmount
Sent from my MI 5s On ebrahimhassan121 notifications@github.com, 27 Jun 2020 8:07 pm wrote:
@angelo558https://github.com/angelo558 did you find a solution
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/realm/realm-js/issues/2765#issuecomment-650593755, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AO5LOWQ2CITRI5FTCW23ZDLRYYYOFANCNFSM4LSDU4WQ.
@Joshfindit That tutorial is out of date. We now have a package called
@realm/react
that handles this better. Try it out and let us know what you think! https://github.com/realm/realm-js/tree/master/packages/realm-react