Help: App error after Update with new myData variable

So, in my apps I have this code:

main.lua :

... local utility = require( "utility" ) local myData = require( "mydata" ) ..... myData.settings = utility.loadTable("settings.json") if myData.settings == nil then myData.settings = {} myData.settings.soundOn = true myData.settings.musicOn = true myData.settings.tutorial = true ........

and, myData.lua :

M = {} M.maxLevels = 50 M.levelScore = 0 M.products = {} M.settings = {} M.settings.currentLevel = 1 ...... return M

so, when I update (in the next version) a new variable (e.g. M.alarmTrigger = false in myData,lua and myData.settings.alarmTrigger = false in main.lua) and I update from my device, the app is error… but if I reinstall it will works normally.

Does the app read main.lua or myData.lua when the first time app is used?

How the app is updated? is the main.lua updated? is the myData.lua updated? how about the settings.json table will do after update?

So, what to do if I modify the settings but no need for reinstalling the app?

Thank you :slight_smile:

If you change parameters after you’ve saved the file the first time, your startup code needs to patch the saved file for each update or you’re find missing keys which are nil which generate errors:

After you do:

myData.settings = utility.loadTable("settings.json") if myData.settings == nil then     myData.settings = {}     myData.settings.soundOn = true     myData.settings.musicOn = true     myData.settings.tutorial = true     myData.settings.alarmTrigger = false  -- make sure to add it for new users     ...     utility.saveTable( myData.settings, "settings.json") end

You probably could do:

local needsReSaved = false -- v1.2 Added alarmTrigger if not myData.settings.alarmTrigger then      myData.settings.alarmTrigger = false needsReSaved = true end -- v1.3 Added SuperMicroBus if not myData.settings.superMicroBus then     myData.settings.superMicroBus = "yellow" needsReSaved = true end if needsReSaved then utility.saveTable( myData.settings, "settings.json" ) end

Then each time you update the app include any settings blocks in main.lua and update the in-memory settings table and if needed save it back out. Keep in mind people might not update every version and you have to plan for that.

Rob

If you change parameters after you’ve saved the file the first time, your startup code needs to patch the saved file for each update or you’re find missing keys which are nil which generate errors:

After you do:

myData.settings = utility.loadTable("settings.json") if myData.settings == nil then     myData.settings = {}     myData.settings.soundOn = true     myData.settings.musicOn = true     myData.settings.tutorial = true     myData.settings.alarmTrigger = false  -- make sure to add it for new users     ...     utility.saveTable( myData.settings, "settings.json") end

You probably could do:

local needsReSaved = false -- v1.2 Added alarmTrigger if not myData.settings.alarmTrigger then      myData.settings.alarmTrigger = false needsReSaved = true end -- v1.3 Added SuperMicroBus if not myData.settings.superMicroBus then     myData.settings.superMicroBus = "yellow" needsReSaved = true end if needsReSaved then utility.saveTable( myData.settings, "settings.json" ) end

Then each time you update the app include any settings blocks in main.lua and update the in-memory settings table and if needed save it back out. Keep in mind people might not update every version and you have to plan for that.

Rob