cordova文件检查异步写入

时间:2016-03-25 12:29:00

标签: javascript cordova asynchronous ionic-framework

我使用 cordovaFile 方法来测试文件是否存在于多个目录中。当我找到一个时,我会做一些动作并打破for循环。

但是,cordovaFile.checkFile(比如writeFile或其他方法)异步工作,所以CLI显示我(对于console.log):

1     890383   log      ===== file_created =====
2     890389   log      object
3     890395   log      null
5     890492   log      ===== file_created =====
6     890494   log      object
7     890496   log      null
9     890556   log      ===== file_created =====
10    890558   log      object
11    890559   log      null
13    890626   log      ===== file_created =====
14    890627   log      object
15    890628   log      null
17    890671   log      ===== file_created =====
18    890672   log      object
19    890674   log      null

有没有办法同步运行cordova.checkFile?

我的代码(源自控制器):

dir_list = [
    cordova.file.applicationDirectory,
    cordova.file.externalRootDirectory,
    cordova.file.externalDataDirectory,
    cordova.file.applicationStorageDirectory,
    cordova.file.dataDirectory
];

for (index = 0; index < dir_list.length; ++index) {
    current_dir = dir_list[index];

    file_created = null;
    $cordovaFile.checkFile(current_dir, filename)
    .then(function (success) {
        file_created = true;
    }, function (error) {
        file_created = false;
    }

    console.log('===== file_created =====');
    console.log(typeof file_created);
    console.log(JSON.stringify(file_created));

    if (file_created) {
        // Some actions
        break;
    }
});

灵感来自Aaron Franco的答案

// This function will run by backup function
$scope.write_file_list = function(file_list, filename, content) {
  if (file_list.length) {
    current_dir = file_list[0];
    $cordovaFile.writeFile(current_dir, filename, content, true).then(
    function(result) {
      // Action if file saved
    }, function(err) {
      file_list.splice(0, 1);
      $scope.write_file_list(file_list, filename, content);
    });
  }
  else {
    // Action if none file saved
  }

};

// This function is called by controller
$scope.backup = function() {
    dir_list = [
        cordova.file.applicationDirectory,
        cordova.file.externalRootDirectory,
        cordova.file.externalDataDirectory,
        cordova.file.applicationStorageDirectory,
        cordova.file.dataDirectory
    ];
    content = 'content file';
    filename = 'test.txt';
    $scope.write_file_list(dir_list, filename, content);
}

1 个答案:

答案 0 :(得分:1)

您应该在创建文件后可以调用的方法中执行代码。

dir_list = [
    cordova.file.applicationDirectory,
    cordova.file.externalRootDirectory,
    cordova.file.externalDataDirectory,
    cordova.file.applicationStorageDirectory,
    cordova.file.dataDirectory
];

for (index = 0; index < dir_list.length; ++index) {
    current_dir = dir_list[index];
    $cordovaFile.checkFile(current_dir, filename)
    .then(function (success) {
        console.log('===== file_created =====');
        // do some stuff here that deals with the created file.
    }, function (error) {
        // do something with error
    }

});

通过这种方式,不需要标记文件创建。您知道该文件是在调用成功函数时创建的。

Cordova旨在在您的应用程序的主线程上运行,因此您同步执行的任何操作都将阻止该线程。这就是Cordova应用程序异步编写的原因。这和JavaScript开始时的异步语言这一事实​​。

替代方案是使用承诺。

<script src="https://www.promisejs.org/polyfills/promise-7.0.4.min.js"></script>

您应该能够以这种方式使用它来检查一个文件。

dir_list = [
    cordova.file.applicationDirectory,
    cordova.file.externalRootDirectory,
    cordova.file.externalDataDirectory,
    cordova.file.applicationStorageDirectory,
    cordova.file.dataDirectory
];
function checkIfFileExist(filename) {
   return new Promise(function(resolve, reject){
     for (index = 0; index < dir_list.length; ++index) {
       current_dir = dir_list[index];
       $cordovaFile.checkFile(current_dir, filename).then(resolve, reject);
     }
   });
 }

checkIfFileExist(filename).then(function (successData) { 
    // file exists here
}, function (err) { 
    // file doesn't exist here
});

这应该允许您一次检查一个。有关使用承诺的更多详细信息:

http://ramkulkarni.com/blog/using-javascript-promises-with-cordova/