Unable to add array data into realm


(Mohit) #1

I am trying to add array data in 3 properties defined in realm schema but when I create and adding array data into realm then my app crashes without giving any error.

const allPodcasts = {
name: ‘StudentPodcasts’,
properties: {
mostListenedData: ‘double?[]’,
mostLikedPodcasts: ‘double?[]’,
trendingPodcasts: ‘double?[]’,
}
}

addRealmData = () => {
Realm.open({ schema: [allPodcasts], schemaVersion: 3 })
.then(realm => {
realm.write(() => {
let addData = realm.create(‘StudentPodcasts’, {
mostListenedData: [],
mostLikedPodcasts: [],
trendingPodcasts: [],
});
addData.mostListenedData.push(this.state.mostListenedData);
addData.mostLikedPodcasts.push(this.state.mostLikedPodcasts);
addData.trendingPodcasts.push(this.state.trendingPodcasts);
});
})
.catch(error => {
console.log(error);
});
}

Please anybody could suggest why my app crashes when I try to add array into the realm?
I am new to the realm.


(Kenneth Geisshirt) #2

What error message do you see?

Is this.state.mostListenedData a number?


(Mohit) #3

@kneth when I try to add array in realm my app closes every time without giving any error, is my code is right or I am doing any mistake?
In all mostListeneddata, mostLikedPodcasts and trendingPodcasts states having JSON array of 25 objects, which are fetched up with API.


(Kenneth Geisshirt) #4

@themohit You probably have to iterate over the 25 objects and push the values individually.


(Mohit) #5

@kneth I am trying to store one by one object in the loop but after storing every object, when I try to get stored items I got nothing.
Could you please check my updated code and when I try to store 1 item only without loop then the realm will store the item successfully and I got stored item easily, but this is not possible with for loop.

Realm.open({ schema: [mostListened], schemaVersion: 13 })
.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,
});
}
});
})
.catch(error => {
console.log(error);
});


(Mohit) #6

I am sorry @kneth for not responding, thank you so much for helping, the realm is now successfully storing api fetched data.