Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Interruption possible when writing to a file?
Started by bjoern Jun 29 2018 01:24 AM

1 reply to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

bjoern

[GLOBAL: userInfoPane.html]
bjoern
  • Enthusiast

  • 55 posts
  • Corona SDK

Hello,

is it possible that the app's execution gets interrupted/terminated when writing to a file, like in the follow example:

-- Data (string) to write
local saveData = "My app state data"
 
-- Path for the file to write
local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )

-- Open the file handle
local file, errorString = io.open( path, "w" )
 
--------[[ INTERRUPTION/TERMINATION BETWEEN HERE ]]---------

if not file then
    -- Error occurred; output the cause
    print( "File error: " .. errorString )
else
    -- Write data to file
    file:write( saveData )

------------------[[ AND HERE ]]-----------------

    -- Close the file handle
    io.close( file )
end
 
file = nil

Because I noticed that the file gets erased just with the io.open( path, "w" ).

So if the app gets terminated before writing the content and closing the file, the data would be lost.

I know that probability is quite low, but is it technically possible? And if yes, is there a way to avoid this problem?

 

Best regards!



[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,458 posts
  • Corona SDK

I ran into this issue with an SQL lite database, so I always made a backup copy of the file before doing anything with it. If the data became corrupted, I could then restore from the backup copy.




[topic_controls]
[/topic_controls]