Is "File.write(content)" executed async?

You must Login before you can answer or comment on any questions.

I'm wondering, because if I write a downloaded JSON file to the filesystem and try to decode it right after writing, the JSON parser throws an exception. If I try do decode it again (without newly writing to the filesystem) it works on the exact same file.

Does the function file.write() return immediately, thus before the filewriting-process has finished?

— asked 1 year ago by Jonas Walti
1 Comment
  • I have the same problem too f.write() then f.read() right after it is not working......

    — commented 11 months ago by Lawrence Cheuk

2 Answers

If your code is as such:

xhrFunctionToGetFile();
ioFunctionToReadFile();
Then you will experience this problem. If however, you call your write function from inside the onload method of the httpClient, your io operations will not start until after a successful download.

— answered 1 year ago by Stephen Feather
answer permalink
1 Comment
  • Hi Stephen Thanks for your answer. I do everything inside the onload callback. Here some code, maybe it helps.

    This is what I do directly in the 'xhr.onload' callback:

    myApp.utils.writeJSONFile(localPathFromAppDir,this.responseText);
    Ti.App.fireEvent('fileDownloaded',{fileName:localPathFromAppDir});
    In writeJSONFile() I just do this:
    var newFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
    newFile.write(content);
    And in the handler of the event 'fileDownloaded', I read the file and try to parse is as mentioned before.

    I have really no idea what's going wrong here... Thanks for any hints...

    — commented 1 year ago by Jonas Walti

You may check if file is really written successfully first... if(f.write("sth")===false){

}

— answered 11 months ago by Lawrence Cheuk
answer permalink
1 Comment
  • I had the same issue with corrupt write operations when storing images. I presume the issue stems from beginning a new write operation to a new file before an existing write operation has completed (e.g. multiple loaders inside a loop).

    If my onload callback is pointing to an external function the writes will corrupt. If it's an inline function it will write successfully.

    Strangely the file.write(this.responseData) will always === false even after a successful write operation. For now I've used an inline callback to resolve the issue.

    var loadHandler = function() {
        // write file to filesystem... often results in corrupt write operation e.g. only partially written image.
        var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'uniqueFilename.jpg');
        file.write(this.responseData);
    }
     
    while(condition) {
        var xhr = Ti.Network.createHTTPClient({
                //onload: loadHandler, // results in corrupt files
                onload: function(e) {
                // write file to filesystem... results in successful write operation.
                var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'uniqueFilename.jpg');
                file.write(this.responseData);
                },
                onerror: function(e) {
                    // Handle error
                },
                timeout: 5000
        });
     
    }

    — commented 7 months ago by Tim Keir

Your Answer

Think you can help? Login to answer this question!