javascript - IndexedDB 和 Angular 应用程序上的大量插入

标签 javascript json angularjs indexeddb dexie

我正在努力处理来自服务器的 20-50k JSON 对象响应,我应该将其插入到我们的 indexeddb 数据存储中。

使用 foreach 重复响应,并添加每一行。响应少于 10k 行的调用工作正常,并在一分钟左右的时间内插入。但是当金额变大时,数据库在一段时间后变得无响应并返回此错误消息

"db Error err=transaction aborted for unknown reason"

我正在为数据库使用 Dexie 包装器,并为 dexie 使用名为 ngDexie 的 Angular 包装器。

var deferred = $q.defer();
var progress = 0;
 // make the call
 $http({
       method: 'GET',
       headers: headers,
       url: '/Program.API/api/items/getitems/' + user
            }).success(function (response) {

        // parse response
        var items = angular.fromJson(response);
        // loop each item
        angular.forEach(items, function (item) {    
                    // insert into db
                    ngDexie.put('stuff', item).then(function () {
                        progress++;
                        $ionicLoading.show({
                            content: 'Loading',
                            animation: 'fade-in',
                            template: 'Inserting items to db: ' + progress
                                       + '/' + items.length,
                            showBackdrop: true,
                            maxWidth: 200,
                            showDelay: 0

                        });

                        if (progress == items.length) {
                            setTimeout(function () {
                                $ionicLoading.hide();
                            }, 500);
                            deferred.resolve(items);

                        }      
                    });
                });
            }).error(function (error) {
                $log('something went wrong');
                $ionicLoading.hide();
            });
            return deferred.promise;

我在处理一个 block 中的整个数据时是否采用了错误的方法?是否有更好的替代方案?整个过程仅在用户打开站点时完成一次。非常感谢所有帮助。目标设备是运行 Android 和 Chrome 的平板电脑。

最佳答案

由于您收到未知错误,因此 I/O 出现问题。我的猜测是下面的数据库在处理数据量时遇到困难。可以尝试分批拆分,每批最多 10k。

A transaction can fail for reasons not tied to a particular IDBRequest. For example due to IO errors when committing the transaction, or due to running into a quota limit where the implementation can't tie exceeding the quota to a partcular request. In this case the implementation MUST run the steps for aborting a transaction using the transaction as transaction and the appropriate error type as error. For example if quota was exceeded then QuotaExceededError should be used as error, and if an IO error happened, UnknownError should be used as error.

您可以在specs中找到它

另一种可能性,您是否在对象存储上定义了任何索引?因为对于您拥有的每个索引,每次插入都需要维护该索引。

关于javascript - IndexedDB 和 Angular 应用程序上的大量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33594790/

相关文章:

javascript - 使主要内容在移动屏幕上占据全宽

javascript - 阻止 Javascript 中的 Windows 键

javascript - 另一个尝试在dojo上注册带有id的小部件

javascript - 如何使用Windows身份验证连接Sql express?

javascript - 是否可以使用包含多个单词的字符串作为 json key?

php - 如何使用php将mysql数据库表数据转换成json

python - Pyparsing:将半JSON嵌套明文数据解析为列表

angularjs - ng-repeat 中的 Angular UI 工具提示未关闭

javascript - 配置一个从单元测试中的延迟返回 promise 的 stub 工厂?

javascript - ng隐藏模型更改