简体   繁体   English

几乎同时在workligth JSONStore中保存多次

[英]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.

相关问题 多功能几乎相同 - multiple function almost the same 一次多次拨打同一服务 - Call same service multiple times at the same time Click 事件同时被触发多次 - Click Event is fired multiple times at the same time 如果两次过滤相同的数据,为什么一次执行Array.filter比$ filter花费将近十倍的时间? - If filtering same data twice, why does Array.filter takes almost 10 times more time than $filter if executed at once? HTML5 JS同时播放多次相同的声音 - HTML5 JS play same sound multiple times at the same time 使用相同属性在异步存储中多次保存值 - Save values multiple times with same properties in async storage mongoose post init 钩子中的并行保存错误。 无法并行多次保存()同一个文档 - Parallel save error in mongoose post init hook. Can't save() the same doc multiple times in parallel 同时使用两个几乎完全相同的javascript文件 - Using two almost identical javascript files at the same time 为什么在循环中调用 readFile 时几乎同时调用回调? - Why is the callback called at almost the same time when the readFile is called in a loop? 有没有更好的方法来处理多个if语句几乎相同的功能? - Is there any better way to handle this function with multiple if statements doing almost the same?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM