Data Persists in Corona Simulator But Not On iOS Device (GGdata)

I am using GGData in order to save user data that I want to persist between user sessions of my application after my application has been closed and reopened. When I run my application in the simulator, the user data persists after I close or relaunch the simulator. However, when building and testing my application on an iPhone iOS device, the user settings do not persist. In other words, when I close the application completely, and reopen it the application does not preserve any of the user settings I had altered manually during the previous session. So far, I have not been able to find any reason online why user data would persist on the simulator, but not when running an application on a device. Below is a sample of the code I have been using in my main.lua file to save user data that I want to persist between user sessions:

local myData = require( “myData” )

local GGData = require( “GGData” )

local box = GGData:new( “storage” )

local function defaultValues()

myData.soundIndex = 1
myData.uiBackgroundIndex = 1

myData.exitCount = nil

end

defaultValues()

local function saveBox()

box.uiBackgroundIndex = myData.uiBackgroundIndex
box.soundIndex = myData.soundIndex

box.exitCount = myData.exitCount

box:save()

end

local function setValues()

myData.uiBackgroundIndex = box.uiBackgroundIndex

myData.soundIndex = box.soundIndex

myData.exitCount = box.exitCount

end

local function onSystemEvent( event )

if ( event.type == “applicationStart” ) then

if box.exitCount ~= nil then
        setValues()
end

elseif ( event.type == “applicationExit” ) then

if myData.exitCount == nil then
            print(“first time closed”)
            myData.exitCount = 1
            print( "exitCount = "…myData.exitCount)
else
            myData.exitCount = myData.exitCount + 1
            print( "exitCount = "…myData.exitCount)
end
      
saveBox()

end

end

Runtime:addEventListener( “system”, onSystemEvent )

I don’t know GGData but I would suspect this is because it is storing the data in the temp directory and not documents.

Thanks for your post! I thought that that might be the case also. However, by default, the function "local box = GGData:new( “storage” )" is supposed to save/load data in the system.DocumentsDirectory location, if a path is not specified (see the explanation for the function below). I would try overriding the default values of this function by manually inputting the baseDir as “system.DocumentsDirectory”, but I am not sure what use in this function for the path input_._

— Initiates a new GGData object.
@param id The name of the GGData to create or load ( if it already exists ).
@param path The path to the GGData. Optional, defaults to “boxes”.
@param baseDir The base directory for the GGData. Optional, defaults to system.DocumentsDirectory.
@return The new object.
function GGData:new( id, path, baseDir )

I would try saving by using GGData and then later load the same file using your own code directly from the docs folder, in the same session (without restarting the app) and see if the file is where you expect it to be. Maybe it’s just being moved when the system exits. Is there an option in GGData to clean certain files, maybe associated with file extensions?

Whenever I’ve used GGData, I’ve saved the box whenever I make changes.

http://forums.coronalabs.com/topic/29409-applicationexit-event-not-triggered-on-device/

This discussion would appear to indicate that force closing the app (i.e double click home then swipe up) will not give an applicationExit event.

I use DMC Autostore now, mainly because any changes are saved immediately without any code required. So you could make you myData table an autostore table and any changes made will persist without any need to ‘save’.

http://docs.davidmccuskey.com/display/docs/Quick+Guide±+dmc_autostore

I appreciate the responses to my post! I tried calling my saveBox() function during the application suspend event and other places in my code to save user data, after reading the nick_sherman’s article about the application exit event not being triggered when the application is force closed (by double pressing the home button then swiping up). This solved my problem, and user data appears to be saved now after force-closing and reopening my application on my iPhone. Thanks again.

Don’t forget to mark the post which solved your problem.

I don’t know GGData but I would suspect this is because it is storing the data in the temp directory and not documents.

Thanks for your post! I thought that that might be the case also. However, by default, the function "local box = GGData:new( “storage” )" is supposed to save/load data in the system.DocumentsDirectory location, if a path is not specified (see the explanation for the function below). I would try overriding the default values of this function by manually inputting the baseDir as “system.DocumentsDirectory”, but I am not sure what use in this function for the path input_._

— Initiates a new GGData object.
@param id The name of the GGData to create or load ( if it already exists ).
@param path The path to the GGData. Optional, defaults to “boxes”.
@param baseDir The base directory for the GGData. Optional, defaults to system.DocumentsDirectory.
@return The new object.
function GGData:new( id, path, baseDir )

I would try saving by using GGData and then later load the same file using your own code directly from the docs folder, in the same session (without restarting the app) and see if the file is where you expect it to be. Maybe it’s just being moved when the system exits. Is there an option in GGData to clean certain files, maybe associated with file extensions?

Whenever I’ve used GGData, I’ve saved the box whenever I make changes.

http://forums.coronalabs.com/topic/29409-applicationexit-event-not-triggered-on-device/

This discussion would appear to indicate that force closing the app (i.e double click home then swipe up) will not give an applicationExit event.

I use DMC Autostore now, mainly because any changes are saved immediately without any code required. So you could make you myData table an autostore table and any changes made will persist without any need to ‘save’.

http://docs.davidmccuskey.com/display/docs/Quick+Guide±+dmc_autostore

I appreciate the responses to my post! I tried calling my saveBox() function during the application suspend event and other places in my code to save user data, after reading the nick_sherman’s article about the application exit event not being triggered when the application is force closed (by double pressing the home button then swiping up). This solved my problem, and user data appears to be saved now after force-closing and reopening my application on my iPhone. Thanks again.

Don’t forget to mark the post which solved your problem.