Saving data - works on Simulator but not on the device

Code is below. When quitting and restarting the app in the simulator, the score data is retreived. On the iPad, it is not. Is there some kind of build setting I need to set for this to work on real devices?

[code]
if( event.type == “applicationExit” ) then

–create a file to save current state
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory );
local file = io.open( path, “w+b” );

local scores = “1,2,3”;
file:write( scores );
io.close( file );

elseif ( event.type == “applicationStart” ) then
–check for saved data
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory );
local file = io.open( path, “r” );

if file then
print(“loading previous state variables…”);
local contents = file:read( “*a” );

io.close( file );

else
print(“did not find file.”);
end

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

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

end

end

[/code] [import]uid: 52127 topic_id: 12169 reply_id: 312169[/import]

Bumping this in hopes for some guidance - I still can’t get this to work on the device. Is there a build setting I’m missing? [import]uid: 52127 topic_id: 12169 reply_id: 45298[/import]

You don’t need to change any settings. the above code should work as such.
I tried and its working for me.

[lua]local function onSystemEvent(event)
if( event.type == “applicationExit” ) then

–create a file to save current state
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory );
local file = io.open( path, “w+b” );

local scores = “1,2,3”;
file:write( scores );
io.close( file );

elseif ( event.type == “applicationStart” ) then
–check for saved data
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory );
local file = io.open( path, “r” );

if file then
print(“loading previous state variables…”);
local contents = file:read( “*a” );
print(contents)
io.close( file );

else
print(“did not find file.”);
end

end

end

Runtime:addEventListener( “system”, onSystemEvent )[/lua]

If its not… the only reason I suspect is your application is exiting too fast before it can save any data. the system won’t wait until you finish saving.

try running the below code and see how it works. let me know the result.

[lua]local function saveItem(event)
if event.phase ==“ended” then
–create a file to save current state
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory );
local file = io.open( path, “w+b” );

local scores = “1,2,3,4”;
file:write( scores );
io.close( file );
end
end

local function readItem(event)
if event.phase ==“ended” then
–check for saved data
local path = system.pathForFile( “scores.txt”, system.DocumentsDirectory );
local file = io.open( path, “r” );

if file then
print(“loading previous state variables…”);
local contents = file:read( “*a” );
print(contents)
io.close( file );

else
print(“did not find file.”);
end
end
end
local saveTXT = display.newText(“Save”, 200, 200, nil, 55)
local readTXT = display.newText(“Read”, 200, 310, nil, 55)

saveTXT:addEventListener(“touch”, saveItem )
readTXT:addEventListener(“touch”, readItem )[/lua] [import]uid: 71210 topic_id: 12169 reply_id: 45304[/import]

Well, I have determined that the application exits too fast and does not save the data when you use the applicationExit event listener, which means that I have to save the data manually at arbitrary points during the game.

Is that what you guys do? Is the applicationExit listener useless then? [import]uid: 52127 topic_id: 12169 reply_id: 45483[/import]

Theoretically this should work and I use to get my data saved like this.

I remember reading somewhere that in application exit we are just supposed to do “Save and exit”… no variable declarations and all…

you can try doing some simple tweaking to make the saving part as simple as possible.

try putting the scores = “1,2,3,4”; outside the applicationExit event and also declare the path and file outside the event.

please try the below code and see how it works

[lua]local path
local file
local scores = “1,2,3”;

local function onSystemEvent(event)
if( event.type == “applicationExit” ) then
–create a file to save current state
path = system.pathForFile( “scores8.txt”, system.DocumentsDirectory );
file = io.open( path, “w+b” );
file:write( scores );
io.close( file );
elseif ( event.type == “applicationStart” ) then
–check for saved data
path = system.pathForFile( “scores8.txt”, system.DocumentsDirectory );
file = io.open( path, “r” );

if file then
print(“loading previous state variables…”);
local contents = file:read( “*a” );
print(contents)
io.close( file );
else
print(“did not find file.”);
end
end
end
Runtime:addEventListener( “system”, onSystemEvent )[/lua]
[import]uid: 71210 topic_id: 12169 reply_id: 45558[/import]