cordova 3.0 FileWriter THREAD WARNING: exec() call to File.write blocked the main thread...should use CordovaInterface.getThreadPool()
Asked Answered
V

1

6

I'm using the FileWriter and it works fine except for these messages in the logcat when I write largish files of various sizes upto about 3MB.

I had a look at the FileUtils.java source and the write function doesn't use the getThreadPool() interface (the reader does).

As a test I thought I'd adapt the filewriter to use the runnable interface and was able to get the code to compile and execute - unfortunately the logcat messages still show up...

The blocking times I get are anything between 25ms and 1200ms so far. I haven't run any serious comparison tests to determine if this change makes any real difference - I was just looking for the absence of logcat messages.

Would these changes as below make any real difference?

Are these message something I should worry about?

My java is pretty basic - but here are the changes I made - following the reader implementation.

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));

} */

And in the write function as below...

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));
        }
    }
});

}

Vivianviviana answered 9/8, 2013 at 4:8 Comment(0)
C
0

Yes, these messages are important and you should use background thread for complicated tasks, such as file writing. The reason of this problem is that these tasks are blocking cordova and you could experience for example UI lags.

If your next actions are dependent on this task being completed, I recommend you using callback method.

Costrel answered 4/5, 2014 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.