I have schema like below:
const MMSchema = {
name: 'MM',
primaryKey: 'art',
properties: {
art: 'int',
tar: {type: 'string', optional: true},
p_date: {type: 'string', optional: true},
p_time: {type: 'string', optional: true},
article: {type: 'string', optional: true},
design_no: {type: 'string', optional: true},
grp: {type: 'string', optional: true},
color: {type: 'string', optional: true},
composition: {type: 'string', optional: true},
width: {type: 'int', optional: true},
weight: {type: 'int', optional: true},
barcode: {type: 'string', optional: true},
drm: {type: 'string', optional: true},
d_date: {type: 'string', optional: true},
adet: {type: 'int', optional: true},
c_adet: {type: 'int', optional: true},
ey: {type: 'int', optional: true},
gy: {type: 'int', optional: true},
ys: {type: 'int', optional: true},
}
};
sample input:
{"mmArray": [
{
"art": 2,
"tar": "2011-01-21T00:00:00Z",
"p_date": "2016-01-19T00:00:00Z",
"p_time": "16:51:21",
"article": "HI-MULTI CHIFFON",
"design_no": "",
"grp": "",
"color": "",
"composition": "100%PES",
"width": 150,
"weight": 55,
"barcode": "K00000002",
"drm": "A",
"d_date": "2011-01-21T00:00:00Z",
"adet": 1,
"c_adet": 0,
"ey": 3,
"gy": 5,
"ys": 8
},
{
"art": 4,
"tar": "2011-01-21T00:00:00Z",
"p_date": "2015-04-04T00:00:00Z",
"p_time": "11:31:59",
"article": "SPUN VISCOSE",
"design_no": "",
"grp": "",
"color": "",
"composition": "100%VI",
"width": 150,
"weight": 125,
"barcode": "K00000004",
"drm": "A",
"d_date": "2011-01-21T00:00:00Z",
"adet": 1,
"c_adet": 0,
"ey": 3,
"gy": 5,
"ys": 8
}
]}
It works:
realm.write(() => {
realm.create('MM', response.mmArray[0], true);
});
It doesn't work.
error message: Array must contain values for all object properties
realm.write(() => {
realm.create('MM', response.mmArray, true);
});;
How can I write batch ?
Just to be clear, your first example with response.mmArray[0] works alright but batch insertion does not? If so that is because we don't yet support batch insertion. There is an issue for this here #242
Until we support this you could insert using a loop or one of the array iteration methods:
realm.write(() => {
response.mmArray.forEach((values) => {
realm.create('MM', values, true);
});
});
Yes, batch doesnt work. Ok I will try loop. When do you provide batch insert? Do you have any roadmap for this ? Thanks.
Not yet sure when we will add this. At the moment there isn't a significant performance gain when batching vs looping within a single transaction. In both cases we only do one write to disk and the overhead in the js layer are comparable.
20k items take ~4 mins, is it normal ?
That seems pretty slow. Are you creating all the items in a single transaction? If performing a transaction for each object it could definitely take this long.
I just tried out 20000 items using your model in the benchmark example. It took 1.8 seconds to create and insert on device. Sounds like you are either doing multiple transactions or you have a bottleneck somewhere else in your app.
Here is my code
Api.GetItems().then((response) => {
realm.write(() => {
console.log("start: " + new Date().toISOString());
for(var i = 0; i < response.mmArray.length; i++){
realm.create('MM', response.mmArray[i], true);
}
console.log("done: " + new Date().toISOString());
});
});
Whats wrong?
Not sure what is wrong as your code looks fine. If you are running in chrome debug mode that could slow things down considerably.
Yes, Chrome debug mode was the issue. After disabling chrome debug mode, 20k items take ~2 seconds. Thanks !
Most helpful comment
Just to be clear, your first example with
response.mmArray[0]works alright but batch insertion does not? If so that is because we don't yet support batch insertion. There is an issue for this here #242Until we support this you could insert using a loop or one of the array iteration methods: