2

I'm just getting started with Realm IO for React Native, and I have started with the following example code:

const Realm = require('realm');

class ReactNative_GrammarApp extends Component {
 render() {
   let realm = new Realm({
    schema: [{name: 'Dog', properties: {name: 'string'}}]
 });

realm.write(() => {
 realm.create('Dog', {name: 'Rex'});
 realm.create('Dog', {name: 'Bert'});
 realm.create('Dog', {name: 'Sam'});
 realm.create('Dog', {name: 'John'});
 realm.create('Dog', {name: 'Simon'});
 realm.create('Dog', {name: 'Larry'});
 realm.create('Dog', {name: 'Seymor'});     
});

  return (
   <View style={styles.container}>
    <Text style={styles.welcome}>
     Count of Dogs in Realm: {realm.objects('Dog').length}
    </Text>
   </View>
  );

 }
}

My issue is that every time I refresh the app in the simulator then the count grows by 7. I can see why this would be happening in the code, but how would I go about creating a database that does not double in size every time I refresh the app? My experience is with things like MySQL, so this is pretty strange for me.

1 Answer 1

1

Refreshing via developer menu behaves kinda like killing the app and open it again. Every time the component gets rendered you are writing those elements again to your database. And of cause it gets rendered every time you open your app. (It is always the same database. The database get's not recreated on app start!)

Additional Comment: You will kinda never ever write something to your database in the render function.

Sign up to request clarification or add additional context in comments.

5 Comments

To reset the database remove the application from your device and reinstall the app or check the Realm documentation on how to remove all item from the database. It is also possible to delete all 'Dog'-Entries of cause.
If a person would not normally write something to their database in the render function, could you show an example of how they would normally write something to their database?
The question should not be "how", it should be "when". You will write data to the database on events like the user submits a formula or you got a response from a server request or something like that.
If you do it in the render method it will write data everytime the Component gets rendered, this is the case if it mounted, the props changed or the state changed for example (there are more cases when the render method gets called)
To fill your database on first app launch you might want to set componentDidMount method of your App Component in combination with a flag whether the app was launched before in your local storage. See the answer of following question for an idea of how it works. stackoverflow.com/questions/40715266/… (Use-case is a little bit different, but the way you do it is kinda similar)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.