【问题标题】:cordova 3.0 FileWriter THREAD WARNING: exec() call to File.write blocked the main thread...should use CordovaInterface.getThreadPool()cordova 3.0 FileWriter 线程警告:对 File.write 的 exec() 调用阻塞了主线程...应该使用 CordovaInterface.getThreadPool()
【发布时间】:2013-08-10 23:38:08
【问题描述】:

我正在使用 FileWriter,当我编写各种大小高达约 3MB 的大型文件时,除了 logcat 中的这些消息之外,它工作正常。

我查看了 FileUtils.java 源代码,写函数不使用 getThreadPool() 接口(读者使用)。

作为一个测试,我认为我会调整文件编写器以使用可运行接口,并且能够让代码编译和执行 - 不幸的是 logcat 消息仍然显示...

到目前为止,我得到的阻塞时间在 25 毫秒到 1200 毫秒之间。我没有进行任何认真的比较测试来确定此更改是否会产生任何真正的影响 - 我只是在寻找 logcat 消息的缺失。

以下这些更改会产生真正的影响吗?

这些消息是我应该担心的吗?

我的 java 非常基础 - 但这里是我所做的更改 - 在阅读器实现之后。

else if (action.equals("write")) {
    this.write(args.getString(0), args.getString(1), args.getInt(2), args.getBoolean(3), callbackContext);
}
/* this is the original code
else if (action.equals("write")) {
    long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2), args.getBoolean(3));
    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));

} */

并且在下面的写函数中...

public void write(String filename, final String data, final int offset, final boolean isBinary, final CallbackContext callbackContext) throws FileNotFoundException, IOException, NoModificationAllowedException {
if (filename.startsWith("content://")) {
    throw new NoModificationAllowedException("Couldn't write to file given its content URI");
}

final String fname = FileHelper.getRealPath(filename, cordova);

this.cordova.getThreadPool().execute(new Runnable() {
    public void run() {
        Log.d(LOG_TAG, "Starting write");
        try {
            boolean append = false;
            byte[] rawData;
            if (isBinary) {
                rawData = Base64.decode(data, Base64.DEFAULT);
            } else {
                rawData = data.getBytes();
            }
            ByteArrayInputStream in = new ByteArrayInputStream(rawData);
            FileOutputStream out = new FileOutputStream(fname, append);
            byte buff[] = new byte[rawData.length];
            in.read(buff, 0, buff.length);
            out.write(buff, 0, rawData.length);
            out.flush();
            out.close();
            Log.d(LOG_TAG, "Ending write");
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, rawData.length));
        } catch (IOException e) {
            Log.d(LOG_TAG, e.getLocalizedMessage());
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, NOT_READABLE_ERR));
        }
    }
});

}

【问题讨论】:

    标签: android cordova threadpool filewriter


    【解决方案1】:

    是的,这些消息很重要,您应该将后台线程用于复杂的任务,例如文件写入。出现此问题的原因是这些任务阻塞了 cordova,您可能会遇到 UI 滞后等问题。

    如果你接下来的动作依赖于这个任务的完成,我建议你使用回调方法。

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 2012-11-09
      • 2016-11-29
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多