I had encountered this issue with my own code, so this time I "borrowed" the loadsave code from Corona's documentation in order to make sure it wasn't just my code.
When I first start the app on simulator (or start it after having wiped the project's sandbox), there are no issues. I create and save a table called "data" as JSON.
The problems arise when I start the app again and the data.json file exists in the project's Documents directory. The table is loaded correctly, but if I try to save it again immediately, then I am met with the following error message: "File error: C:\....\Documents\data.json: Permission denied". According to the loadsave.lua file's M.saveTable function, the file simply does not exist. However, if I try to save the table after a long delay by using timer.performWithDelay, like 15-20s, then the file does exist and everything works.
What is specifically the cause of this issue and how could I address it?
I have attached a sample project that demonstrates the issue. Just comment the rows 27 and 29 in main.lua to toggle between instant and delayed saving.
Best Answer SGS , 16 January 2019 - 04:50 PM
I don't get why you need to open the file and then open it again in the save block? My code worked perfectly every time without this extra (and seemingly pointless) step.
You might have more luck if you close the file after the first open and before the second open.
-- if the file is loaded, then we run into issues if file then saveData = loadTable( "data.json" ) io.close(file) <-- close before opening it later! end
I imagine, because you haven't closed the file after the first read it remains locked for some OS specific time.