Unable to Save file on Device

Hey guys,

I’m not able to save a file on the device. When I run the code to save the file and I test on the device I get a black screen. When I test in the corona simulator it works just fine. Can anyone tell me why my screen is going black? Everything works fine when I do not require the file to load and save…

My Code:

module(..., package.seeall)  
  
local \_M = {};  
  
local json;  
local savefile;  
  
function \_M:init()  
  
 json = require("json");  
 savefile = system.pathForFile( "playerdata.txt", system.DocumentsDirectory );  
  
 -- Create a save file if it doesn't exist --  
 local file = io.open(savefile,"r");  
 if(file)then  
 else  
 file = io.open(savefile,"w");  
 local userdata = json.encode(self:getFreshPlayer());  
 file:write(userdata);  
 io.close(file);  
 end  
  
end  
  
-- FUNCTIONS -------------------------------------------------  
  
function \_M:saveData(playerdata)  
 local file = io.open(savefile,"w");  
 local userdata = json.encode(playerdata);  
 file:write(userdata);  
 io.close(file);  
end  
  
function \_M:loadData()  
 local file = io.open(savefile,"r");  
 local data = file:read("\*a");  
 local datadecode = json.decode(data);  
 io.close(file);   
 return datadecode;  
end  
  
function \_M:getFreshPlayer()  
 local player = {  
  
 curr\_goobers = 0,  
 life\_goobers = 0,  
 score = 0,  
 stage = 1,  
 level = 1,  
 numkills = 0,  
 numpasses = 0,  
 adventure\_complete = false,  
 turret = {  
 active = false,  
 ammo\_level = 1,  
 radius\_level = 1  
 },  
 ammo = {  
 true,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false  
 },  
 perks = {  
 false,false,false,false  
 },  
 survival = {  
 total\_waves = 0  
 }  
  
  
 };  
 return player;  
  
end  
  
return \_M;  

Thanks! [import]uid: 63800 topic_id: 14561 reply_id: 314561[/import]

Did you ever solve this problem?
I’m using JSON to save user data between uses, and it works on the simulator (both Corona and XCode) and on Android, but on iOS it doesn’t work.
I’ve put a 10 second timer in to save the data (just for testing it) which goes through the code below among other thing.

require "json"  
count = count + 1;  
resetText.text = "Save "..count;  
  
local incomepath = system.pathForFile("incomeTables.json");  
count = count + 1;  
resetText.text = "Save "..count;  
  
local incomefh= io.open(incomepath, "w");  
count = count + 1;  
resetText.text = "Save "..count;  
  
incomefh:write(json.encode(\_G.incomeMainTable));  
count = count + 1;  
resetText.text = "Save "..count;  
  
io.close(incomefh);  
count = count + 1;  
resetText.text = "Save "..count;  

It only manages to increase ‘count’ once on my 3GS, so it must have a problem with the “system.pathForFile” part. I’ve tried adding the system.DocumentsDirectory parameter but still ‘count’ only reaches 1, so it must be ignoring the rest. I should note that I have this in one function, and at the end it has a symbol which changes from red to green - on the device it never reaches that part, so the function must be crashing - however all other on screen functions work perfectly.

Is anyone able to help?

Edit: I should add that if I manually change the data in the JSON file using dashcode, the device still reads it fine, so the problem must lie in the writing to file. [import]uid: 84115 topic_id: 14561 reply_id: 75511[/import]

@alan,

First, you MUST either use system.DocumentsDirectory or system.TemporaryDirectory when writing files on devices. The default (if you don’t specify it is system.ResourceDirectory, which you will NOT be able to write to on the device.

Second, try changing our open line to also get the errorStr like this:

[lua]local incomefh, errStr = io.open( incomepath, “w” )

if ( incomefh == nil ) then
print( "Reason open failed: " … errStr )
end[/lua]

You can either print it like above, and have your device attached to your mac, using the Xcode organizer to look at the console output, or you can do a newText() call and display the error on the screen (just for debug purposes).

Ken [import]uid: 16734 topic_id: 14561 reply_id: 75931[/import]

Thanks Ken,

Gave it a quick try and while I don’t get any error message output - it still doesn’t save as it should.

Should I be making a ‘Documents’ folder within my app’s main folder or something like that, or is the location of system.DocumentDirectory handled by the app itself.
By that I mean, my JSON files are in the main folder, so how does the program know if it’s in the Recource, Document or Temp directory?

Thanks [import]uid: 84115 topic_id: 14561 reply_id: 76177[/import]

I eventually just started using SQL instead of files. It turned out to be much easier to use anyhow as you can save individual items and recall individual records easier. I never found a solution but I did read somewhere that we might be restricted from writing to the device in AdHoc or when previewing games so it should technically work once the game is coming from the app store. [import]uid: 63800 topic_id: 14561 reply_id: 76192[/import]