基于 IOS 10.3.1 cordova 的应用程序在尝试写入 IndexedDB 时崩溃

标签 ios cordova crash offline indexeddb

我正在开发一个基于cordova的多平台Web应用程序,使用sapui5框架v1.44和indexedDB来存储数据。该应用程序工作正常,直到上次ios更新10.3.1,现在在尝试写入时崩溃到索引数据库。我正在使用 put 方法来更新数据,并且我对应用程序进行了全新安装。我尝试写入indexedDB的代码框架是这样的:

    writeToIDB: function (objStoreName, result, success, error) {
    //Asynchronous function
    var defer = Q.defer();
    var res = [];
    if (!!result && Array.isArray(result)) {
        res = result;
    } else if (!!result && result.hasOwnProperty("results") && Array.isArray(result.results)) {
        res = result.results;
    } else if (!!result && typeof result === 'object') {
        res.push(result);
    }
    if (res.length >= 0) {
        if (window.myDB) {
            if (!window.myDB.objectStoreNames.contains(objStoreName)) {
                console.log("ObjectStore for " + objStoreName + " doesn't exist");
                if (error) {
                    error("ko")
                } else {
                    defer.reject("ko");
                }
            } else {
                var oTransaction = window.myDB.transaction([objStoreName], "readwrite");
                var oDataStore = oTransaction.objectStore(objStoreName);
                oTransaction.oncomplete = function (event) {
                    console.log("Transaction completed: database modification for " + objStoreName + " finished.");
                    if (success) {
                        success();
                    } else {
                        defer.resolve("ok");
                    }

                };
                oTransaction.onerror = function (event) {
                    console.log("Transaction for " + objStoreName + " not opened due to error. Check for duplicate items or missing properties!");
                    console.log(event.target.error);
                    if (error) {
                        error("ko")
                    } else {
                        defer.reject("ko");
                    }

                };
                var oRecord = {};
                for (var i = 0; i < res.length; i++) {
                    oRecord = res[i];
                    oDataStore.put(oRecord);
                }
            }
        } else {
            this.createIDB().then(
                function (resCreate) {
                    console.log("DB Created successfully");
                    if (!window.myDB.objectStoreNames.contains(objStoreName)) {
                        console.log("ObjectStore for " + objStoreName + " doesn't exist");
                        if (error) {
                            error("ko")
                        } else {
                            defer.reject("ko");
                        }
                    } else {
                        var oTransaction = window.myDB.transaction([objStoreName], "readwrite");
                        var oDataStore = oTransaction.objectStore(objStoreName);
                        oTransaction.oncomplete = function (event) {
                            console.log("Transaction completed: database modification for " + objStoreName + " finished.");
                            if (success) {
                                success();
                            } else {
                                defer.resolve("ok");
                            }
                        };
                        oTransaction.onerror = function (event) {
                            console.log("Transaction for " + objStoreName + " not opened due to error. Check for duplicate items or missing properties!");
                            console.log(event.target.error);
                            if (error) {
                                error("ko")
                            } else {
                                defer.reject("ko");
                            }
                        };
                        var oRecord = {};
                        for (var i = 0; i < res.length; i++) {
                            oRecord = res[i];
                            oDataStore.put(oRecord);
                        }
                    }
                }.bind(this),
                function (err) {
                    console.log("DB Creation failed");
                    if (error) {
                        error("ko")
                    } else {
                        defer.reject("ko");
                    }
                }.bind(this)
            );
        }
    } else {
        if (error) {
            error("ko")
        } else {
            defer.reject("ko");
        }
    }
    if (typeof success === 'undefined' && typeof error === 'undefined') {
        return defer.promise;
    }


},

P.S.我省略了部分代码。 这在以前版本的 ios 上工作得很好,我想我已经安装了 10.2.1,现在它在调用 put 方法后崩溃了。我现在尝试将ios升级到10.3.2的测试版,但结果是一样的。其他人注意到这一点或知道如何解决这个问题吗? 谢谢 K

更新 我发现了问题:复杂的数据类型。由于 IndexedDB 支持保存和检索复杂的数据类型,因此我有一些属性,它们是我用来保存在一些对象存储中的数组或对象。这对我来说绝对是一个大问题,因为我能想到的唯一解决方法是对复杂的字段进行处理,但由于我处理大量数据,这会产生很大的性能问题。我希望ios开发团队能够尽快找到解决方案

最佳答案

您确定 res[] 数组中的每个键都是有效键吗?这里有一个已关闭的错误:

https://bugs.webkit.org/show_bug.cgi?id=170000

看起来如果你传入无效的 key 将会导致 webkit 崩溃。

此问题的修复可能会包含在 iOS 的下一个公开版本中。

要确定有效 key ,请参阅 W3.org 规范的这一部分:

3.1.3 Keys

In order to efficiently retrieve records stored in an indexed database, each record is organized according to its key. A value is said to be a valid key if it is one of the following ECMAScript [ECMA-262] types: Number primitive value, String primitive value, Date object, or Array object. An Array is only a valid key if every item in the array is defined and is a valid key (i.e. sparse arrays can not be valid keys) and if the Array doesn't directly or indirectly contain itself. Any non-numeric properties on an Array are ignored, and thus do not affect whether the Array is a valid key. If the value is of type Number, it is only a valid key if it is not NaN. If the value is of type Date it is only a valid key if its [[PrimitiveValue]] internal property, as defined by [ECMA-262], is not NaN. Conforming user agents must support all valid keys as keys.

这是从这里获取的:

https://www.w3.org/TR/IndexedDB/#key-construct

关于基于 IOS 10.3.1 cordova 的应用程序在尝试写入 IndexedDB 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43524860/

相关文章:

ios - 我们如何在另一个类中的数组或字典数据中获取 SBJSON connectionDidFinishLoading 方法

eclipse - 在项目 'Faceted Project Validation Builder' 上运行构建器 'MyProject' 时出错

heroku - 应用程序崩溃 : active_support/dependencies. rb:251:in `require':无法加载此类文件 - JSON(LoadError)

IOS - 生成本地镜像的 MD5 时内存耗尽

objective-c - UISlider 自定义

iphone - 每分钟点击次数 iOS 编程

objective-c - 将背景图像设置为uitableview?

css - Phonegap 移动应用程序的独立 CSS 模板

android - 在 Crosswalk Cordova for Android 应用程序中启用 Cookie

崩溃后 Android 小部件无响应/不会更新