首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >cordova 3.0 FileWriter线程警告:对File.write的exec()调用阻止了主thread...should使用CordovaInterface.getThreadPool()

cordova 3.0 FileWriter线程警告:对File.write的exec()调用阻止了主thread...should使用CordovaInterface.getThreadPool()
EN

Stack Overflow用户
提问于 2013-08-09 12:08:41
回答 1查看 3.4K关注 0票数 6

我使用的是FileWriter,它工作得很好,除了当我写各种大小到大约3MB的大文件时,日志中的这些消息。

我看了一下写的源代码,FileUtils.java函数没有使用getThreadPool()接口(阅读器使用)。

作为一个测试,我认为我应该调整文件写入器以使用runnable接口,并能够编译和执行代码-不幸的是,logcat消息仍然显示……

到目前为止,我得到的阻塞时间在25ms到1200ms之间。我还没有运行任何认真的比较测试来确定这个更改是否真的有什么不同--我只是在寻找是否没有logcat消息。

下面的这些改变会有什么真正的不同吗?

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

我的java是非常基础的--但下面是我在阅读器实现之后所做的修改。

代码语言:javascript
运行
复制
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));

} */

在write函数中,如下所示...

代码语言:javascript
运行
复制
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));
        }
    }
});

}

EN

回答 1

Stack Overflow用户

发布于 2014-05-04 15:50:52

是的,这些消息很重要,你应该使用后台线程来处理复杂的任务,比如文件写入。这个问题的原因是这些任务阻塞了cordova,例如,你可能会遇到UI延迟。

如果您的下一步操作依赖于此任务的完成,我建议您使用回调方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18139898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档