folderitem.delete sometimes returns 101

Here is some code from my app:

[code] f = GetFolderItem (filename, FolderItem.PathTypeNative)
if (f.Exists=false) then
// Do something else, like return, exit, or continue
end if

  f.delete ()
  if  (f.LastErrorCode>0)  then
    msg = "unable to delete file: " + filename
    writeLog (msg)
  end if

[/code]

Generally, the file is deleted, but sometimes when it deletes it, the LastErrorCode is 101 which means “File not found”. Anything I need to know here? The file does exist and should be owned by the user - it will have been written recently by the app itself, as follows:

[code]binstr = BinaryStream.Create (atFile, false)
if (binstr=Nil) then // Couldn’t open the new file
msg = “error, could not create new file”
me.task.UIwriter (1, msg)
return
end if

result = dbreadBlob (me.dbhimg, absid, image, imagelen) // Here, I read a blob from an SQLite database
if (result<>app.READ_OK) then
msg = “error, could not read file from database”
me.task.UIwriter (1, msg)
return
end if

binstr.write (image) // Write image out
binstr.flush ()
binstr.close () // Wrote the file out OK, can close it now
[/code]

The deletion may only be requested a few minutes after the file is created. Should I be doing something like binstr = NIL ? This is on OS X and the Finder shows the file as in its folder, so I assume it has been written out - but who knows.

Could it be the file is still open? or the cache has not yet been flushed and there for the file is still more “promised to be saved” than actually “saved” (though that state should only last <1 second)

Does the file actually still show after the error 101 ? Or is it deleted anyway ?

I explicitly close the file after writing it - not sure what more I can do. And Finder shows the file as disappearing from the folder it’s in, so it seems to be gone, even though I get the 101 error.

You aren’t doing this in side a DropBox (or other file sharing system) folder are you?

No. It’s all on the user’s local disk.

Are you deleting using a loop? It may be that the file indeed doesn’t exist if you’ve deleted items before it and it’s moved up in the list.

For instance, if you were using a for-next, try using a for-each

I’m getting this as well. I’m displaying a graphic in a window and giving the user the option to delete it.
f.delete
works … the file is deleted but lastErrorCode always seems to be set to 101. Have checked the code before and after the delete. Before it’s 0 and after its 101, and the file is gone.

Any idea what I’m doing wrong or is this just a quirk of folderItem.

Jack