Reputation: 756
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.
Upvotes: 2
Views: 331
Reputation: 901
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.
Upvotes: 1