PhoneGap的/科尔多瓦的FileWriter与读写器不工作 [英] phonegap/cordova filewriter and reader not working

查看:102
本文介绍了PhoneGap的/科尔多瓦的FileWriter与读写器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eclipse上开发Android应用程序与科尔多瓦2.2.0。似乎得到的PhoneGap的文件API,但不能读取或将文件写入。

I'm developing an Android app on eclipse with cordova 2.2.0. It seems that gets Phonegap's file API, but can't read or write on the files.

我复制了X code,在那里我与应用程序适用于iOS完成了剧本,和它的作品。

I've copied the script from xcode, where I'm finished with the app for iOS, and it works.

下面是我的脚本,与控制台输出追踪:

Here's my script, traced with console outputs:

window.onload = function (){
    console.log('1: onload');
    document.addEventListener("deviceready", getSettings, false);
}
function getSettings(){
    console.log('2: getSettings()');
    fileSys('settings.txt', 'getContent', null);
    //fileSys('settings.txt', 'replaceContent', 'new settings');
}
function fileSys(fileName, action, data){
    console.log('3: fileSys - '+fileName+' - '+action);
    var directory = (fileName == 'sidur') ? 'appin/sidur':'appin';
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    function gotFS(fileSystem) {
    console.log('4: Got file system, get directory...');
    fileSystem.root.getDirectory(directory, {create: true}, gotDir, fail);
    }
    function gotDir(dirEntry) {
        console.log('5: Got directory. Get file...');
        dirEntry.getFile(fileName, {create: true, exclusive: false}, gotFileEntry, fail);
    }
    function gotFileEntry(fileEntry){
        console.log('6: got file. Perform action: '+action+'...');
        if(action == 'getContent') readAsText(fileEntry);
        if(action == 'replaceContent') fileEntry.createWriter(gotFileWriter, fail);
    }
    function gotFileWriter(writer){
        console.log('7: got file writer...');
        writer.write(data); //function variable of fileSys();
        writer.onwriteend = function(evt) {
        console.log('8: file written');
        };
    }
    function readAsText(file) {
        console.log('7: read as text...');
        var reader = new FileReader();
        reader.readAsText(file);
        reader.onloadend = function(evt) {
            console.log('9: done reading file');
            init(evt.target.result);
        }
    }
    function fail(error){
        console.log('fail: '+error.code);

    }
}
function init(settings){
    console.log('Init. Settings: '+JSON.stringify(settings));
}

运行此脚本提供了以下控制台输出:

Running this script gives the following console output:


  • 1:onload事件

  • 2的getSettings()

  • 3的filesys - SETTINGS.TXT - 的getContent

  • 4:得到的文件系统,获取目录...

  • 5:明白目录。获取文件...

  • 6:拿到文件。执行操作:...的getContent

  • 7:读文本...

还有停止。 reader.onloadend不会被调用,并且没有指定错误。如果我再次运行,而是调用的filesys('SETTINGS.TXT','replaceContent','新设置');和outcomment其他调用的filesys,控制台输出:

And there it stops. reader.onloadend is never called, and no error is specified. If I run again, but instead call fileSys('settings.txt', 'replaceContent', 'new settings'); and outcomment the other call to fileSys, the console outputs:


  • 1:onload事件

  • 2的getSettings()

  • 3的filesys - SETTINGS.TXT - replaceContent

  • 4:得到的文件系统,获取目录...

  • 5:明白目录。获取文件...

  • 6:拿到文件。执行操作:replaceContent ...

  • 7:拿到文件写入...

我有:


  • 设置正确的权限/在res / config.xml和在Android的manifest.xml插件

  • 验证了PhoneGap的API包含和工作(与通知)

我是新的应用程序开发,以及日食,所以这很可能是我已经错过了一些基本的东西。任何建议和指针是最欢迎的。

I am new to app development as well as eclipse, so this could very well be some basic thing I've missed. Any suggestions and pointers are most welcome.

推荐答案

其实,这是不是你的语句的顺序,因为这两个Android和iOS将在同一顺序间preT他们。所不同的是在该readAsText完成时,由于其工作在另一个线程异步发生的速度。下面是iOS上发生了什么事的例子:

Actually, it is NOT the order of your statements, because both Android and iOS will interpret them in the same sequential order. What is different is the speed at which the readAsText completes, because its work happens asynchronously in another thread. Here's an example of what happened on iOS:

reader.readAsText - this starts the read process in another thread
reader.onloadend = function... - you set up your handler
-- on separate thread, readAsText finally completes and sees your handler and calls it

这是发生了什么事在Android:

this is what happened on Android:

reader.readAsText - this starts the read process in another thread
-- on separate thread, readAsText completed quickly but your handler has not been set yet so it does not get called
reader.onloadend = function... - you set up your handler too late, the read already completed in its own thread

在异步调用,你不能保证在其他任务完成。这不是Android和iOS之间的差异,它的多线程操作的只是性质。有各种各样的方式来处理异步调用(延迟订货和嵌套回调得当,使用jQuery,一些类型的信号量机制,更多的人,我敢肯定的)。最主要的是,永远,永远,依靠或承担的任务将完成在一定的时间。时序依赖会咬你大的时候,是非常难以调试。

In asynchronous calls, you can't guarantee when other tasks complete. It's not a difference between Android and iOS, it's just the nature of multi-threaded operations. There are all kinds of ways to deal with asynchronous calls (ordering and nesting callbacks properly, using jquery deferred, some type of semaphore mechanism, and many more, I'm sure). The main thing is, never, ever, rely on or assume that tasks will complete in a certain time. Reliance on timing can bite you big time and is VERY difficult to debug.

这篇关于PhoneGap的/科尔多瓦的FileWriter与读写器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆