Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
anphorea
Posts: 134
Registered: ‎11-27-2012
My Device: Z10 LE,Dev Alpha C

File remove sometimes doesn't remove the file.

Here's the code:

    window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(window.PERSISTENT, 5 * 1024 * 1024, function(fs) {
        fs.root.getFile(path, {create: false}, function(fileEntry) {
            fileEntry.remove(function() {
                console.log('FILE DELETED。');
            }, function(ex) {
                console.log(ex);
            });
        }, function(ex) {
            console.log(ex);
        });
    }, function(f) {
        console.log(f);
    });

 this code would print "file deleted" in the console,but the file is still there.(note: this code would run multiple times during app runtime.

 

however,the code below works:

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(window.PERSISTENT, 5 * 1024 * 1024, function(fs) {
        fs.root.getFile(path + "home.json", {create: false}, function(fileEntry) {
            fileEntry.remove(function() {
                console.log(path + 'home.json DELETED。');
            }, function(ex) {
                console.log(ex);
            });
        }, function(ex) {
            console.log(ex);
        });
        fs.root.getFile(path + "content.json", {create: false}, function(fileEntry) {
            fileEntry.remove(function() {
                console.log(path + 'content.json DELETED。');
            }, function(ex) {
                console.log(ex);
            });
        }, function(ex) {
            console.log(ex);
        });
        fs.root.getFile(path + "question.json", {create: false}, function(fileEntry) {
            fileEntry.remove(function() {
                console.log(path + 'question.json DELETED。');
            }, function(ex) {
                console.log(ex);
            });
        }, function(ex) {
            console.log(ex);
        });
        fs.root.getFile(path + ".jpg", {create: false}, function(fileEntry) {
            fileEntry.remove(function() {
                console.log(path + '.jpg 文件已删除。');
            }, function(ex) {
                console.log(ex);
            });
        }, function(ex) {
            console.log(ex);
        });
    }, function(f) {
        console.log(f);
    });

 I don't know why,it's kind of weird,is there any suggestion?

 

should I keep the filesystem object in memory and get the filesystem only once?

/*
WebWorks Developer
*/
BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10

Re: File remove sometimes doesn't remove the file.

I'm not sure that I'm seeing the difference between the top code snipped and bottom except that in the top, we're only deleting one file and in the bottom has multiple files.

 

Is the top code snippet being executed inside of a loop by chance? (i.e. you are cycling through an array of paths to delete?)

 

In general, I would also recommend avoiding what you have in the second part because you could get concurrent attempts to access the filesystem which could cause breaks. You should only ever be accessing the filesystem / File APIs one at a time.

 

Personally, I would recommend the use of callbacks when a file is successfully deleted (or a failure occurs) to move onto the next file. This will ensure you're only ever attempting to delete one file at a time, and not concurrently trying to delete two files.

 

As your code is written though, unless there is more to it, I'm not sure why the top wouldn't work but the bottom would.


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Developer
anphorea
Posts: 134
Registered: ‎11-27-2012
My Device: Z10 LE,Dev Alpha C

Re: File remove sometimes doesn't remove the file.

Hi oros
So file api can only be accessed one at a time? Why?
I don't understand
/*
WebWorks Developer
*/
BlackBerry Development Advisor
oros
Posts: 1,557
Registered: ‎04-12-2010
My Device: BlackBerry Z10

Re: File remove sometimes doesn't remove the file.

[ Edited ]

It seems this issue may not be as far reaching as I originally thought. From the spec:

http://www.w3.org/TR/FileAPI/

 

8.5.1. Multiple Reads

The FileReader interface makes available three asynchronous read methods - readAsArrayBuffer, readAsText, and readAsDataURL, which read files into memory. If multiple concurrent read methods are called on the same FileReader object, user agents must throw an InvalidStateError [DOM4] on any of the read methods that occur when readyState = LOADING.

 

And:

 

Error conditions can occur when reading files from the underlying filesystem. The list below of potential error conditions is informative.

The File or Blob being accessed may not exist at the time one of the asynchronous read methods or synchronous read methods are called. This may be due to it having been moved or deleted after a reference to it was acquired (e.g. concurrent modification with another application). See NotFoundError

A File or Blob may be unreadable. This may be due to permission problems that occur after a reference to a File or Blob has been acquired (e.g. concurrent lock with another application). Additionally, the snapshot state may have changed. See NotReadableError

 

For the most part, this is in reference to trying to access Files or Blobs if they have been modified by another source.

 

My understanding was that there was a more generic File API lock that basically prevented concurrent access to a much higher degree. I myself ran into some issues recently when trying to perform the following concurrently:

  • window.resolveLocalFileSystemURI
  • window.requestFileSystem
  • Cordova FileTransfer.upload

I was expecting each to return asynchronously but only the last would ever return (the other two would appear to do nothing.) When executed individually, each returned successfully. The explanation I received was that the File API itself (in general) had limitations in what it could accomplish concurrently.

 

EDIT: I'll try to narrow down more specifics on any particular limitations.


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.
Developer
anphorea
Posts: 134
Registered: ‎11-27-2012
My Device: Z10 LE,Dev Alpha C

Re: File remove sometimes doesn't remove the file.

thanks
I'm using different filetransfer and file reader objects but still can't make it work concurrently.
So I have to make a queue for it.
Too bad
/*
WebWorks Developer
*/
Developer
gpl4all
Posts: 68
Registered: ‎01-23-2012
My Device: Z10, Q10 & Playbook 64Gb

Re: File remove sometimes doesn't remove the file.

i have no problem at all removing file prior to put new content to it, this is my code:

ncFSWriteFile = function (ncArgFS) {
	console.log (Date.now().toString() + ' in ncFSWriteFile');
	ncFSR = window.webkitRequestFileSystem (LocalFileSystem.PRESISTENT, 0, function (ncFSR) {
		ncFSR.root.getFile(ncArgFS, {create: true, exclusive: false}, ncFSRemoveFile, ncFSfail);
	}, ncFSfail);
}

ncFSRemoveFile = function (ncArgFileEntry) {
	ncArgFileEntry.remove (function () {
			ncFS = window.webkitRequestFileSystem (LocalFileSystem.PRESISTENT, 0, function (ncFS) {
				ncFS.root.getFile(ncd.configfile, {create: true, exclusive: true}, ncFSWgotFileEntry, ncFSfail);
			}, ncFSfail);	
		}, ncFSfail);
}

ncFSWgotFileEntry = function (ncArgFileEntry) {
	ncArgFileEntry.createWriter(ncFSWwriter, ncFSfail);
}

ncFSWwriter = function (ncArgWriter) {
  // Create a new Blob and write it to file
  var ncjBlob = new Blob([JSON.stringify(ncd.credential)], {type: 'application/json', endings: 'native'});
	ncArgWriter.onwriteend = function (evt) {
		console.log("writen");
	}
	ncArgWriter.write(ncjBlob);
}

ncFSfail = function (ncArgFSerror) {
  var msg = '';
  switch (ncArgFSerror.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;	
	
	}
	console.log("Error " + msg);
}

function ncFSWriteFile is call to write file, as a matter of fact, it first calls ncFSRemoveFile, which right after done with  file remove gets in the real write sequence.

 

the only different i've seen is i use window.webkitRequestFileSystem instead of window.requestFileSystem and to be honest, i haven't got change to find out the difference between them, i simply use window.webkitRequestFileSystem because window.requestFileSystem didn't work in my app and too bad i forget what the error it produced.

 

 

 

 

New Developer
abinlesly
Posts: 105
Registered: ‎06-11-2013
My Device: DEVELOPER

Re: File remove sometimes doesn't remove the file.

File issues are common . Make sure that you use a using clause which will ensure this.

******Blackberry Developer*******---Built for the Kill-----
#######Developer of @Sudoku#########