Reputation: 123
I'm designing an offline app using React Native and Realm. One of the features is downloading data from the web API before going offline.The downloaded data is a long JSON. For example:
[{"EventID":"3769","EventName":"Birthday Party","EventDate":"Jun 21 2018 4:00 PM"},{"EventID":"4232","EventName":"Homecoming","EventDate":"Jun 22 2018 11:00 PM"}, {"EventID":"3838","EventName":"the Summer Show","EventDate":"Jun 28 2018 2:00 AM"}]
There are many examples of how to insert one entry in Realm, such as:
realm.write(() => {
Event.push({EventID: 3769, EventName: 'Birthday Party'});
Event.push({EventID: 4232, EventName: 'Homecoming'});
});
But is there a way to bulk insert JSON into a Realm table at once?
Many thanks!!!
Update - Works!
I've tried geisshirt's solution (thanks!) with codes below. It takes about 10sec to import a JSON String with 50,000 entities. (Each entity has ID, name and date properties). One trick is that debug mode is much slower than running on the real app.
React Native Code:
axios.get('https://APISERVER/XX/GET_EVENTS')
.then(response => {
Realm.open({
schema: [{ name: 'Events', properties: { EventID: 'string', EventName: 'string', EventDate: 'string' } }]
}).then(realm => {
realm.write(() => {
response.data.forEach(obj => {
realm.create('Events', obj);
});
});
})
Thanks again!
Upvotes: 6
Views: 6169
Reputation: 11264
Data.js
const DataList= {
list: [
{
"prodId": "4232",
"prodName": "Hero Moto Corp",
"prodDesc": "Allrounder ",
"prodPrice": "15000",
"prodImage": "https://lh3.googleusercontent.com/a-/AAuE7mBq326Bqo0dObU3TDEDK3fBw9kc3PI5J4Tjt9fw=s96-c"
},
{
"prodId": "4232",
"prodName": "Hero Moto Corp",
"prodDesc": "Awesome product",
"prodPrice": "18500",
"prodImage": "https://lh3.googleusercontent.com/a-/AAuE7mBq326Bqo0dObU3TDEDK3fBw9kc3PI5J4Tjt9fw=s96-c"
}
]
}
export default DataList;
// let objs = JSON.parse('[{"prodId":4232,"prodName":"Homecoming","prodDesc":"MBA" , "prodImage": "https://lh3.googleusercontent.com/a-/AAuE7mBq326Bqo0dObU3TDEDK3fBw9kc3PI5J4Tjt9fw=s96-c","prodPrice" : "15236"},{"prodId":4232,"prodName":"Homecoming","prodDesc":"MBA" , "prodImage": "https://lh3.googleusercontent.com/a-/AAuE7mBq326Bqo0dObU3TDEDK3fBw9kc3PI5J4Tjt9fw=s96-c","prodPrice" : "15236"},{"prodId":4232,"prodName":"Homecoming","prodDesc":"MBA" , "prodImage": "https://lh3.googleusercontent.com/a-/AAuE7mBq326Bqo0dObU3TDEDK3fBw9kc3PI5J4Tjt9fw=s96-c","prodPrice" : "15236"},{"prodId":4232,"prodName":"Homecoming","prodDesc":"MBA" , "prodImage": "https://lh3.googleusercontent.com/a-/AAuE7mBq326Bqo0dObU3TDEDK3fBw9kc3PI5J4Tjt9fw=s96-c","prodPrice" : "15236"}]');
import DataList from './Data'
DataList.list.forEach(obj => {
db.addProduct(obj).then((result) => {
console.log(" insert result --------- " + result);
this.setState({
isLoading: false,
});
this.props.navigation.navigate('ProductScreen');
// alert("Sqlite Insert Difference " + difference );
}).catch((err) => {
console.log(" insert err --------- " + err);
this.setState({
isLoading: false,
});
})
});
Upvotes: 0
Reputation: 2497
You can do something like:
let objs = JSON.parse('[{"EventID":"3769","EventName":"Birthday Party","EventDate":"Jun 21 2018 4:00 PM"},{"EventID":"4232","EventName":"Homecoming","EventDate":"Jun 22 2018 11:00 PM"}, {"EventID":"3838","EventName":"the Summer Show","EventDate":"Jun 28 2018 2:00 AM"}]');
realm.write(() => {
objs.forEach(obj => {
realm.create('Event', obj);
});
});
You insert all object in one transaction (realm.write()
).
Upvotes: 6