【问题标题】:MobileFirst Platform JSONStore An error occurred when reading from the databaseMobileFirst Platform JSONStore 从数据库读取时出错
【发布时间】:2023-03-05 01:13:01
【问题描述】:

IBM MobileFirst 版本:6.3 移动操作系统:Android 4.4.4

我有一个用例,我们需要将单个或多个 base64 编码的图像字符串存储在集合内的单个文档中。 (由于用例的性质,无法将图像拆分为集合内的多个文档)

将最多 7 个 base64 编码的图像字符串存储到同一个文档中可以按预期工作。 (将包含 7 个 base64 字符串图像的文档添加到 JSONStore)。 当具有 8 个 base64 编码字符串图像的文档已添加到集合内的同一文档中时。文档已成功添加到集合中。但是当在集合上调用 findAll() 时(它只包含 1 个包含 8 个 base64 编码图像的文档;在这种用例中,需要 findAll() ,因为在数据时不存在填充搜索字段的数据从集合中获取);出现以下错误:

Java:

E/JSONSTORE(24451): JSONStoreLogger.logError in JSONStoreLogger.java:189 :: Error when attempting to find a document. An error occurred when reading from the database.
E/JSONSTORE(24451): com.worklight.jsonstore.exceptions.JSONStoreFindException: Error when attempting to find a document. An error occurred when reading from the database.
E/JSONSTORE(24451):     at com.worklight.jsonstore.api.JSONStoreCollection.findDocuments(JSONStoreCollection.java:896)
E/JSONSTORE(24451):     at com.worklight.androidgap.jsonstore.dispatchers.FindActionDispatcher.databaseActionDispatch(FindActionDispatcher.java:117)
E/JSONSTORE(24451):     at com.worklight.androidgap.jsonstore.dispatchers.BaseDatabaseActionDispatcher.actionDispatch(BaseDatabaseActionDispatcher.java:36)
E/JSONSTORE(24451):     at com.worklight.androidgap.jsonstore.dispatchers.BaseActionDispatcher.dispatch(BaseActionDispatcher.java:90)
E/JSONSTORE(24451):     at com.worklight.androidgap.plugin.DispatchingPlugin$ActionDispatcherRunnable.run(DispatchingPlugin.java:79)
E/JSONSTORE(24451):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/JSONSTORE(24451):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/JSONSTORE(24451):     at java.lang.Thread.run(Thread.java:841)
E/JSONSTORE(24451): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 0 failed
E/JSONSTORE(24451):     at net.sqlcipher.CursorWindow.getLong_native(Native Method)
E/JSONSTORE(24451):     at net.sqlcipher.CursorWindow.getLong(CursorWindow.java:381)
E/JSONSTORE(24451):     at net.sqlcipher.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:110)
E/JSONSTORE(24451):     at net.sqlcipher.AbstractCursor.moveToPosition(AbstractCursor.java:195)
E/JSONSTORE(24451):     at net.sqlcipher.AbstractCursor.moveToNext(AbstractCursor.java:257)
E/JSONSTORE(24451):     at android.database.CursorWrapper.moveToNext(CursorWrapper.java:166)
E/JSONSTORE(24451):     at com.worklight.jsonstore.api.JSONStoreCollection.findDocuments(JSONStoreCollection.java:877)
E/JSONSTORE(24451):     ... 7 more
E/JSONSTORE(24451): JSONStoreLogger.logError in JSONStoreLogger.java:189 :: error while executing find query on database "tempCollection"com.worklight.jsonstore.exceptions.JSONStoreFindException: Error when attempting to find a document. An error occurred when reading from the database.
E/JSONSTORE(24451): com.worklight.jsonstore.exceptions.JSONStoreFindException: Error when attempting to find a document. An error occurred when reading from the database.
E/JSONSTORE(24451):     at com.worklight.jsonstore.api.JSONStoreCollection.findDocuments(JSONStoreCollection.java:896)
E/JSONSTORE(24451):     at com.worklight.androidgap.jsonstore.dispatchers.FindActionDispatcher.databaseActionDispatch(FindActionDispatcher.java:117)
E/JSONSTORE(24451):     at com.worklight.androidgap.jsonstore.dispatchers.BaseDatabaseActionDispatcher.actionDispatch(BaseDatabaseActionDispatcher.java:36)
E/JSONSTORE(24451):     at com.worklight.androidgap.jsonstore.dispatchers.BaseActionDispatcher.dispatch(BaseActionDispatcher.java:90)
E/JSONSTORE(24451):     at com.worklight.androidgap.plugin.DispatchingPlugin$ActionDispatcherRunnable.run(DispatchingPlugin.java:79)
E/JSONSTORE(24451):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/JSONSTORE(24451):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/JSONSTORE(24451):     at java.lang.Thread.run(Thread.java:841)
E/JSONSTORE(24451): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 0 failed
E/JSONSTORE(24451):     at net.sqlcipher.CursorWindow.getLong_native(Native Method)
E/JSONSTORE(24451):     at net.sqlcipher.CursorWindow.getLong(CursorWindow.java:381)
E/JSONSTORE(24451):     at net.sqlcipher.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:110)
E/JSONSTORE(24451):     at net.sqlcipher.AbstractCursor.moveToPosition(AbstractCursor.java:195)
E/JSONSTORE(24451):     at net.sqlcipher.AbstractCursor.moveToNext(AbstractCursor.java:257)
E/JSONSTORE(24451):     at android.database.CursorWrapper.moveToNext(CursorWrapper.java:166)
E/JSONSTORE(24451):     at com.worklight.jsonstore.api.JSONStoreCollection.findDocuments(JSONStoreCollection.java:877)
E/JSONSTORE(24451):     ... 7 more
E/wl.jsonstore(24451): {"src":"find","err":22,"msg":"INVALID_SEARCH_FIELD","col":"tempCollection","usr":"testUser","doc":{},"res":{}}

Javascript:

{"src":"findAll","err":22,"msg":"INVALID_SEARCH_FIELD","col":"tempCollection","usr":"testUser","doc":{},"res":{}}

是我做错了什么,还是我误用了什么?

编辑:

初始化:

var storeDetails = {
    collections: {
        tempCollection: {}
    },
    options: {
        username: 'ausername',
        password: 'apassword',
        localKeyGen: true //Optional local key generation flag, default false.
    }
};

WL.JSONStore.init(storeDetails.collections, storeDetails.options);

全部查找:

var options = {
    limit: 1
};
WL.JSONStore.get('tempCollection').findAll(options);

Base64 编码的图像字符串: 我正在使用 Cordova 相机 API(包含在 Worklight/MF 中)。 代码的sn-p: 通过使用 Camera.DestinationType.DATA_URL,图像以 base64 编码的字符串形式返回。

var options = {
    quality: 100,
    destinationType: Camera.DestinationType.DATA_URL,
    sourceType: Camera.PictureSourceType.CAMERA,
    allowEdit: true,
    encodingType: Camera.EncodingType.PNG,
    targetWidth: 500,
    targetHeight: 500,
    popoverOptions: CameraPopoverOptions,
    saveToPhotoAlbum: true,
    cameraDirection: Camera.Direction.BACK
};

navigator.camera.getPicture(function (imageData) {
    var doc = {
        src: imageData;
    };
    WL.JSONStore.get('tempCollection').add(doc);
}, function (err) {

}, options);

【问题讨论】:

  • 用 init 和 findAll 更新。
  • 你能否包含完整的 JSON Store 实现,其中包含所有内容。
  • 作为我客户的敏感代码,我不能只是复制/粘贴它。我将尝试添加具体信息,您到底在寻找什么?
  • 可能是您将图像转换为 base64 并插入 JSON 存储的方式
  • 提供了更多代码。

标签: android ibm-mobilefirst jsonstore


【解决方案1】:

我能够重现您的问题。在 JSON 文档中添加一些图像作为单个数组的一部分后,我遇到了与您提出的相同的错误。而且,在你显示的错误之前,我可以看到另一个

05-04 20:27:38.934: E/CursorWindow(32148): need to grow: mSize = 1048576, size = 1107180, freeSpace() = 1048482, numRows = 1
05-04 20:27:38.934: E/CursorWindow(32148): Attempting to grow window beyond max size (1048576)
05-04 20:27:38.934: E/Cursor(32148): Failed allocating 1107180 bytes for text/blob at 0,1
05-04 20:27:38.935: D/Cursor(32148): finish_program_and_get_row_count row 0
05-04 20:27:39.151: E/CursorWindow(32148): need to grow: mSize = 1048576, size = 1107180, freeSpace() = 1048482, numRows = 1
05-04 20:27:39.151: E/CursorWindow(32148): Attempting to grow window beyond max size (1048576)
05-04 20:27:39.151: E/Cursor(32148): Failed allocating 1107180 bytes for text/blob at 0,1
05-04 20:27:39.152: D/Cursor(32148): finish_program_and_get_row_count row 0
05-04 20:27:39.152: E/CursorWindow(32148): Bad request for field slot 0,0. numRows = 0, numColumns = 2

所以我猜你是在强调设备内存的限制。

当您将多张图片添加到单个 JSON 文档时,您正在创建一个巨大的 JSON 文档。然后当你搜索它时,可能有某种类型的解析操作,或者类似的东西,需要将整个 JSON 分配到内存中。 findAll 本身的结果将包含那个巨大的 json 文档。并且可能需要使用连续的内存分配。

问题在于单个 Android 应用的可用内存总量是有限的。实际值取决于设备制造商。它不一定与设备的“高端”程度或设备中可用的总内存有关。一些制造商可能会创建具有 2GB 内存但每个应用程序只有几 MB 可用内存的设备,因此他们优先考虑让多个应用程序并行运行而不是几个应用程序占用所有资源的能力。

最后,我的建议是您确实应该将每个图像存储在一个独立的 JSON 文档中。您可以拥有一个包含一组 ID 的文档,并将图像存储在第二个集合中,或者其他一些替代解决方案。

另外请注意,这不是 JSONStore 的限制,而是 Android 平台施加的限制。永远不会允许单个应用单独消耗所有内存资源,它始终被限制为一些 MB。

【讨论】:

  • 感谢详细的解释!我将研究您建议的解决方案并尝试重构我的代码。
  • 添加更多图像时也会失败... JSON Store 使用 Android SQlite db,光标大小限制为 1 MB。因此,如果您查询数据库中的记录,如果查询结果大于 1 MB,您将得到一个异常......这 7 个全高清图像将很容易到达。解决此问题的唯一方法是存储图像信息文件并将指向它们的链接存储在您的数据库中。
猜你喜欢
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 2016-04-27
  • 1970-01-01
  • 2016-07-19
  • 2015-10-06
  • 2023-02-23
  • 1970-01-01
相关资源
最近更新 更多