I am trying to add array data on realm objects, but whenever I try to add data my app crashes without giving any error. I am using fetched data and passing one by one item to each object which has been declared.
import React, { Component } from 'react'
import { Alert, View, Text, TouchableOpacity } from 'react-native';
import { withNavigation } from 'react-navigation';
var Realm = require('realm');
const mostListened = {
name: 'mostListened',
properties: {
id: 'int',
child_image_url: 'string',
child_name: 'string',
children_who_staredA: 'string',
children_who_staredB: 'string',
children_who_staredC: 'string',
children_who_staredD: 'string',
duration: 'int',
number_of_stars: 'int',
number_of_streams: 'int',
title: 'string',
}
}
const mostLiked = {
name: 'mostLiked',
properties: {
id: 'int',
child_image_url: 'string',
child_name: 'string',
children_who_staredA: 'string',
children_who_staredB: 'string',
children_who_staredC: 'string',
children_who_staredD: 'string',
duration: 'int',
number_of_stars: 'int',
number_of_streams: 'int',
title: 'string',
}
}
const trendingPodcasts = {
name: 'trendingPodcasts',
properties: {
id: 'int',
child_image_url: 'string',
child_name: 'string',
children_who_staredA: 'string',
children_who_staredB: 'string',
children_who_staredC: 'string',
children_who_staredD: 'string',
duration: 'int',
number_of_stars: 'int',
number_of_streams: 'int',
title: 'string',
}
}
class AddArrayData extends Component {
static navigationOptions = {
header: null
}
constructor(props) {
super(props);
this.state = {
mostListenedData: [],
trendingPodcasts: [],
mostLikedPodcasts: [],
}
}
componentDidMount() {
fetch('abc')
.then((response) => response.json())
.then((responseJson) => {
this.setState({ mostListenedData: { data: responseJson.data } })
});
fetch('abc')
.then((response) => response.json())
.then((responseJson) => {
this.setState({ mostLikedPodcasts: { data: responseJson.data } })
});
fetch('abc')
.then((response) => response.json())
.then((responseJson) => {
this.setState({ trendingPodcasts: { data: responseJson.data } })
});
}
addStudentData = () => {
console.log(this.state.mostListenedData, this.state.mostLikedPodcasts, this.state.trendingPodcasts);
Realm.open({ schema: [mostListened, mostLiked, trendingPodcasts], schemaVersion: 11 })
.then(realm => {
realm.write(() => {
for (let i = 0; i <= this.state.mostListenedData.data.podcasts.length; i++) {
realm.create('mostListened', {
id: i,
child_image_url: this.state.mostListenedData.data.podcasts[i].child_image_url,
child_name: this.state.mostListenedData.data.podcasts[i].child_name,
children_who_staredA: this.state.mostListenedData.data.podcasts[i].children_who_stared[0],
children_who_staredB: this.state.mostListenedData.data.podcasts[i].children_who_stared[1],
children_who_staredC: this.state.mostListenedData.data.podcasts[i].children_who_stared[2],
children_who_staredD: this.state.mostListenedData.data.podcasts[i].children_who_stared[3],
duration: this.state.mostListenedData.data.podcasts[i].duration,
number_of_stars: this.state.mostListenedData.data.podcasts[i].number_of_stars,
number_of_streams: this.state.mostListenedData.data.podcasts[i].number_of_streams,
title: this.state.mostListenedData.data.podcasts[i].title,
});
realm.create('mostLiked', {
id: i,
child_image_url: this.state.mostLikedPodcasts.data.podcasts[i].child_image_url,
child_name: this.state.mostLikedPodcasts.data.podcasts[i].child_name,
children_who_staredA: this.state.mostLikedPodcasts.data.podcasts[i].children_who_stared[0],
children_who_staredB: this.state.mostLikedPodcasts.data.podcasts[i].children_who_stared[1],
children_who_staredC: this.state.mostLikedPodcasts.data.podcasts[i].children_who_stared[2],
children_who_staredD: this.state.mostLikedPodcasts.data.podcasts[i].children_who_stared[3],
duration: this.state.mostLikedPodcasts.data.podcasts[i].duration,
number_of_stars: this.state.mostLikedPodcasts.data.podcasts[i].number_of_stars,
number_of_streams: this.state.mostLikedPodcasts.data.podcasts[i].number_of_streams,
title: this.state.mostLikedPodcasts.data.podcasts[i].title,
});
realm.create('trendingPodcasts', {
id: i,
child_image_url: this.state.trendingPodcasts.data.podcasts[i].child_image_url,
child_name: this.state.trendingPodcasts.data.podcasts[i].child_name,
children_who_staredA: this.state.trendingPodcasts.data.podcasts[i].children_who_stared[0],
children_who_staredB: this.state.trendingPodcasts.data.podcasts[i].children_who_stared[1],
children_who_staredC: this.state.trendingPodcasts.data.podcasts[i].children_who_stared[2],
children_who_staredD: this.state.trendingPodcasts.data.podcasts[i].children_who_stared[3],
duration: this.state.trendingPodcasts.data.podcasts[i].duration,
number_of_stars: this.state.trendingPodcasts.data.podcasts[i].number_of_stars,
number_of_streams: this.state.trendingPodcasts.data.podcasts[i].number_of_streams,
title: this.state.trendingPodcasts.data.podcasts[i].title,
});
}
});
})
.catch(error => {
console.log(error);
});
Alert.alert("Student Details Added Successfully.")
}
deleteAllData = () => {
Realm.open({ schema: [mostListened, mostLiked, trendingPodcasts], schemaVersion: 11 })
.then(realm => {
realm.write(() => {
realm.deleteAll()
});
})
.catch(error => {
console.log(error);
});
}
showStudentData = () => {
Realm.open({ schema: [mostListened, mostLiked, trendingPodcasts], schemaVersion: 11 })
.then(realm => {
var mostListened = Array.from(realm.objects('mostListened'));
this.state.mostListenedData = mostListened;
this.setState({ mostListenedData: this.state.mostListenedData });
console.warn(this.state.mostListenedData);
})
.catch(error => {
console.log(error);
});
}
render() {
return (
<View style={{ flex: 1 }}>
<TouchableOpacity onPress={() => this.addStudentData()} activeOpacity={0.7} style={{ marginTop: 16, marginLeft: 32, marginRight: 32, justifyContent: 'center', alignItems: 'center', backgroundColor: 'orange', height: 30, width: '40%', alignSelf: 'center', borderRadius: 12 }} >
<Text style={{ color: '#fff', fontSize: 12, fontWeight: 'bold' }}>ADD STUDENT</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.showStudentData()} activeOpacity={0.7} style={{ marginTop: 16, marginLeft: 32, marginRight: 32, justifyContent: 'center', alignItems: 'center', backgroundColor: 'blue', height: 30, width: '40%', alignSelf: 'center', borderRadius: 12 }} >
<Text style={{ color: '#fff', fontSize: 12, fontWeight: 'bold' }}>SHOW STUDENT DATA</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => this.deleteAllData()} activeOpacity={0.7} style={{ marginTop: 16, marginLeft: 32, marginRight: 32, justifyContent: 'center', alignItems: 'center', backgroundColor: 'green', height: 30, width: '40%', alignSelf: 'center', borderRadius: 12 }} >
<Text style={{ color: '#fff', fontSize: 12, fontWeight: 'bold' }}>DELETE ALL DATA</Text>
</TouchableOpacity>
</View>
);
}
}
export default withNavigation(AddArrayData);
Please anybody could suggest that is this the right way to open multiple schemas and with the help of a write method storing data in the realm. I am new to the realm.