Saving / loading works as expected except when I restart DEVICE

I followed the guidelines here:  http://docs.coronalabs.com/guide/data/readWriteFiles/index.html


Below is my code, basically it works exactly as I expect until I restart the device.  I have a ‘score’ table.

score[1] is the current score, and score[2] is the ‘high’ score.

I’m testing on device, and everything works, but when I restart my device, the score[2] defaults back to 0.

Is the save file being deleted on restart?  I have it in the “system.DocumentsDirectory” which states in the CoronaSDK documents that these files should reside for the life of the App.  

I’m testing on an iphone5 ios8… Below is the actual code for the whole saving and loading high score.  It works fine until I restart then the high score ( score[2] ) gets erased/gone.


  local function bestScoreLoadSave()

        

        if ( filePath ) then

            local path = system.pathForFile( “myfile.txt” , system.DocumentsDirectory )

            filePath = io.open( filePath, “r” )

            local savedData = file:read( “*a” )

            io.close( file )

            file = nil

            score[2] = tonumber(savedData)

        end

        if score[2] == nil then

            score[2] = 0

        end

        

        if score[2] < score[1] then

            score[2] = score[1]

            local saveData = tostring(score[2])

            local path = system.pathForFile( “myfile.txt” , system.DocumentsDirectory )

            local file = io.open( path, “w” )

            file:write( saveData )

            io.close( file )

            file = nil

        end

    end

    bestScoreLoadSave()


Why is the high score being reset when the device resets?

Thanks

FYI You can remove the middle IF statement and put this in the first IF:

score[2] = tonumber(savedData) or 0

What do you see in the console if you add a print after the tostring:

local saveData = tostring(score[2]) print(saveData) -- add this to check data

And do you really need to convert to a string there? Can’t you just write the number to the file?

Sorry for the questions and no real answer, but I’m trying to get more info to figure out what is going on. If you could sprinkle some print statements and post the console output it would help.

I’m pretty sure you must convert to string in order to save to file if you use that method.

EDIT ** In my first ‘if statement’ this part:  filePath = io.open( filePath, “r” )

should be: file = io.open( path, “r” )

ahhh stupid me, thanks @horacebury for the help too

FYI You can remove the middle IF statement and put this in the first IF:

score[2] = tonumber(savedData) or 0

What do you see in the console if you add a print after the tostring:

local saveData = tostring(score[2]) print(saveData) -- add this to check data

And do you really need to convert to a string there? Can’t you just write the number to the file?

Sorry for the questions and no real answer, but I’m trying to get more info to figure out what is going on. If you could sprinkle some print statements and post the console output it would help.

I’m pretty sure you must convert to string in order to save to file if you use that method.

EDIT ** In my first ‘if statement’ this part:  filePath = io.open( filePath, “r” )

should be: file = io.open( path, “r” )

ahhh stupid me, thanks @horacebury for the help too