如何将JSON快速插入到React Native应用的Realm数据库中

时间:2018-06-19 21:28:17

标签: react-native realm

我正在使用React Native和Realm设计一个离线应用程序。其中一项功能是在脱机之前从Web API下载数据。下载的数据为长JSON。例如:

[{"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中插入一个条目的例子很多,例如:

realm.write(() => {
  Event.push({EventID: 3769, EventName: 'Birthday Party'});
  Event.push({EventID: 4232, EventName: 'Homecoming'});
});

但是有没有办法一次将JSON批量插入到Realm表中?

非常感谢!


更新-有效!

我尝试使用下面的代码尝试geisshirt的解决方案(谢谢!)。导入具有50,000个实体的JSON字符串大约需要10秒。 (每个实体都有ID,名称和日期属性)。一种技巧是调试模式比在真实应用程序上运行要慢得多。

反应本机代码:

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);
        });
    });
 })

再次感谢!

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

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);
    });
});

您将所有对象插入一个事务(realm.write())。

答案 1 :(得分:0)

<div class="fetched-images">
    <%= image_tag obj["url"], class: "img-fluid", title: obj["title"] , data:{ type: obj["ity"] }, crossOrigin: "Anonymous" %>
    <label class="checkbox-container">
        <input type="checkbox" data-type="<%= file_type %>" data-href="<%= file_url %>" checked="checked" class="this-image">
        <span class="checkmark"></span>
    </label>
</div>

从'./Data'导入DataList

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"}]');