Pjottur
Pjottur

Reputation: 642

Cordova/phonegap filesystem not ready on "deviceready"

I'm developing an ios app with Cordova 2.1.0.

It seems that the fileSystem is not available even if "deviceready" event has fired.

window.onload = function (){
document.addEventListener("deviceready", getSettings(), false);
}

function getSettings(){
fileSys('settings.txt', 'getContent', null);
}

function fileSys(fileName, action, data){
    alert('hello');    // fires
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    alert('hello');    // does not fire
    //rest of the script breaks
}

Script breaks after requesting the filesystem. However, if I wrap the call to fileSys() in a setTimeout, it works. Example:

window.onload = function (){
document.addEventListener("deviceready", getSettings(), false);
}

function getSettings(){
    setTimeout(function(){
    fileSys('settings.txt', 'getContent', null);
}, 500);
}

function fileSys(fileName, action, data){
    alert('hello');    // fires
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    alert('hello');    // fires
    //script runs fine
}

Any solutions to this?

Upvotes: 1

Views: 1082

Answers (3)

Nick Pastuhov
Nick Pastuhov

Reputation: 26

I had to put requestFileSystem function in setTimeout(..., 0) too, but the reason was different.

I run requestFileSystem in class constructor and to make the code which is invoked by a callback in the "success" function work I should have constructor already finished at the moment.

My constructor consists of only the requestFileSystem function, and somehow withot zero setTimeout it finished AFTER it's "success" function.

(only for Android - I didn't notice such expirience on iOS)

Upvotes: 0

Tom Clarkson
Tom Clarkson

Reputation: 16174

The setup for deviceready should be

document.addEventListener("deviceready", getSettings, false);

"getSettings()" means run the function now and pass the result to addEventListener.

"getSettings" means pass a function reference to addEventListener so it can be run when the event fires.

Upvotes: 4

hansvedo
hansvedo

Reputation: 446

Should your fileSys function be prefaced with "function"?

function fileSys(fileName, action, data){}

Upvotes: 2

Related Questions