Problem with io.write causing app to refresh

Hi,

Hoping someone can help with this. This problem only seems to be happening on my Mac, device builds seem to be working ok at the moment.

Basically, when I use io.write, the program ALWAYS restarts 3 times. The code is supposed to store the day the app was last used, and store this in a json file so that the next time it is opened it can check whether it is a new day or not. At the moment this actually checks for a new minute, to make testing easier. The code is below:

[code]
–main.lua

jsonFile = function( filename, base )

– set default base dir if none specified
if not base then base = system.ResourceDirectory; end

– create a file path for corona i/o
local path = system.pathForFile( filename, base )

– will hold contents of file
local contents

– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “r” )
if file then
– read all contents of file into a string
contents = file:read( “*a” )
io.close( file ) – close the file after using it
end

return contents
end

–retrieve previous date from json file
require “json”
timeTable = json.decode(jsonFile(“date.json”));

local function writeCurrentDayToFile()

–if first time running then ensure previous day has a value
if (contents == nil)then contents = “0”;end

–retrieve current time and store as currentDay
local date = os.date( “*t” )
local currentDay = tostring(date.min);
local previousDay = timeTable.time;
print("\nprev: “…previousDay…” and curr: “…currentDay)
print(“seconds til next day:”…(60 - date.sec)…”\n");

–check to see if it is a new day
if(currentDay ~= previousDay)then
print(“new day”);
isNewDay =true;
else
print(“same day”);
end

–write current day to json for next app opening
local path = system.pathForFile( “date.json” )

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

if fh then

local dateTable = {time = tostring(date.min)};
fh:write(json.encode(dateTable));
end
–]]
io.close(fh);

end

writeCurrentDayToFile();

–…main called after al this[/code]
Does anyone have any idea why this happens, every single time. It’s not even a function that’s called every frame, nor does it carry on forever. It’s always 3 times?! The app then runs as normal, however the problem this causes is that when it is a ‘new day’ I only see the new day screen for a split second - unless I’m fortunate enough for the ‘new day’ to fall on the 3rd refresh.

Any help would be greatly appreciated. [import]uid: 84115 topic_id: 16511 reply_id: 316511[/import]

I should also add this extra issue I have noticed with it:

If I use JSON encoding in a system.applicationExit to make sure I only write to the tables when the program closes, on reopening it refreshes like crazy. It seems the more io.write’s I use the more times it refreshes when the app opens.

It’s really irritating more than anything. Oddly if I hit CTRL + R a few times during the refreshing it seems to cancel it out, but it still bypasses my new day screen :frowning: [import]uid: 84115 topic_id: 16511 reply_id: 61709[/import]

Are you writing data to the directory structure of your project? If so, do you have the Corona Simulator set to relaunch whenever the project is modified?

I was having the same problem. I went into the simulator preferences and changed the “Relaunch Simulator when project is modified?” option to “Never.”

The simulator seems to scan the entire filesystem of the directory you’re working out of. Any change to any file (external xml, json, whatever) while you’re running a simulation will cause it to refresh. [import]uid: 90273 topic_id: 16511 reply_id: 66568[/import]

Shoot me now please. Your answer is spot on.
Given that when I manually edited those files it refreshed, you would think that I would have put 2 + 2 together.

Thanks. [import]uid: 84115 topic_id: 16511 reply_id: 66630[/import]

dhishyau

go to preference and set to not refresh every time or take your file to document directory
:slight_smile: [import]uid: 12482 topic_id: 16511 reply_id: 66649[/import]