[英]Save multiple times at almost the same time in workligth JSONStore
I want to add to JSONStore by using a for cycle 我想通过使用for周期添加到JSONStore
so i call the saveToJSON() function inside the for with a length more than 500, but it doesnt add, and in console it shows succes but when i look in the jsonstore theres nothing,and number of the cycle for times that i called to add to jsonstore is in a red bubble in console. 所以我在for内部调用了saveToJSON()函数,长度超过500,但是它没有添加,并且在控制台中显示成功,但是当我在jsonstore中查找时,什么也没有,并且我调用的次数添加到jsonstore在控制台中出现在红色气泡中。
function saveToJSON(object) {
var data ={
title : object.title,
subtitle: object.subtitle,
};
var options = {}; //default
WL.JSONStore.get('MyDataCollection').add(data, options)
.then(function () {
//handle success
console.log("add JSONStore success");
})
.fail(function (errorObject) {
//handle failure
console.log("add JSONStore failure");
});
}
It is not really recommended to perform parallel operations in JSONStore. 确实不建议在JSONStore中执行并行操作。 JSONtore is designed to work asynchronously.
JSONtore设计为异步工作。 You can run JSONStore operations serially by using a for loop.
您可以使用for循环来串行运行JSONStore操作。 However, your example does not show a for loop.
但是,您的示例未显示for循环。 Have you tried to do a for loop using smaller iterations?
您是否尝试过使用较小的迭代进行for循环? Maybe 2 rather than 500.
也许2而不是500。
Try creating an array with the data you want to add and then passing that to JSONStore's add API. 尝试使用要添加的数据创建一个数组,然后将其传递给JSONStore的add API。 Remember to make sure that
WL.JSONStore.init
has finished successfully before calling the add API. 请记住在调用add API之前确保
WL.JSONStore.init
成功完成。
Example pseudocode: 伪代码示例:
//This is the data you want to add, you probably get this from a network call
var someData = [{title: 'hello'}, {title: 'world'}];
//This is an array that you will pass to JSONStore's add API
var someArray = [];
//Populate the array with data you want to pass to JSONStore's add API
for (var i = 0; i < someData.length; i++) {
someArray.push(someData[i]);
}
//Add data inside someArray to the collection called: MyDataCollection
WL.JSONStore.get('MyDataCollection').add(someArray)
.then(function () {
//Do a find all operation on the collection called: MyDataCollection
return WL.JSONStore.get('MyDataCollection').findAll();
})
.then(function (res) {
//Print all the data inside the collection called: MyDataCollection
console.log(JSON.stringify(res));
});
//You may want to add .fail(function(){...}) to handle errors.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.