Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Prevent file overwrite when updating
Started by santiagojrodriguez Feb 22 2019 12:00 PM

5 replies to this topic

Best Answer XeduR @Spyric , 26 February 2019 - 03:30 AM

You may want to check out https://docs.coronalabs.com/guide/data/readWriteFiles/index.html and https://docs.coronalabs.com/guide/data/LFS/index.html

 

I'm not sure if I'm completely following you, but I would just check for one of the files that you plan to create/copy if they are where they are supposed to be, for instance, add the following code to your main.lua:

-- the name of your file that is supposed to be in the DocumentsDirectory
local path = system.pathForFile( "yourfile.txt", system.DocumentsDirectory )
local file = io.open( path, "w" )

if file then
    -- your file is already there
else
    -- move your file there (or create it)
end

This check would run every time your app starts. It's not resource intensive, so no need to worry about that. The only reason why the app wouldn't be there would be if the app is run for the first time, the user has deleted the app's stored files, or something cataclysmic has happened.

If you find the file, don't do anything. If you don't find it, then do your thing.

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

santiagojrodriguez

[GLOBAL: userInfoPane.html]
santiagojrodriguez
  • Observer

  • 13 posts
  • Corona SDK

Hi Guys,
 
I have an Android App that includes a pack of text files. After the app is installed and you play a couple of games, the information in those files will be modified. 
 
If I'm not mistaken, if I published a new update for the app, those files will be automatically overwritten with the same files from the new package. 
Is there any way to prevent this?  I'd like to include the files to be available for anyone who installs the app for the first time, but I don't want my users to loose any progress stored on those files with each update.
 
Thanks in advance.


[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 582 posts
  • Corona SDK

It completely depends on how you where and how those text files are created.

If you store those files in system.DocumentsDirectory, then they will be there even after you update. Then, typically, the safest and smartest method is to always first check if the file exists. If a file exists, then you load it. If it doesn't, then you create it. If you follow those two steps, then you don't need to worry about updates overwriting your text files.



[TOPIC: post.html]
#3

santiagojrodriguez

[GLOBAL: userInfoPane.html]
santiagojrodriguez
  • Observer

  • 13 posts
  • Corona SDK

Hi, thanks for your reply, which made me look deeper into the Android directories organization. I'll probably pack a "initial" set of files with the bundle and move it to DocumentsDirectory if I cannot find them already there.
 
Your approach seems much better than the one I had in mind. My only issue left is: I'd would need to run this code once (just after each installation or update). 
What would be the best way to accomplish that? I was thinking of adding an extra dummy file and deleting it after I do the files check and update you described. Afterwards, I'll check for that file to see if the update was already done or not. But I'm not sure that's the best idea.
 
Thanks.


[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,373 posts
  • Enterprise

FYI: I deleted the duplicate post!



[TOPIC: post.html]
#5

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 582 posts
  • Corona SDK

  Best Answer

You may want to check out https://docs.coronalabs.com/guide/data/readWriteFiles/index.html and https://docs.coronalabs.com/guide/data/LFS/index.html

 

I'm not sure if I'm completely following you, but I would just check for one of the files that you plan to create/copy if they are where they are supposed to be, for instance, add the following code to your main.lua:

-- the name of your file that is supposed to be in the DocumentsDirectory
local path = system.pathForFile( "yourfile.txt", system.DocumentsDirectory )
local file = io.open( path, "w" )

if file then
    -- your file is already there
else
    -- move your file there (or create it)
end

This check would run every time your app starts. It's not resource intensive, so no need to worry about that. The only reason why the app wouldn't be there would be if the app is run for the first time, the user has deleted the app's stored files, or something cataclysmic has happened.

If you find the file, don't do anything. If you don't find it, then do your thing.



[TOPIC: post.html]
#6

santiagojrodriguez

[GLOBAL: userInfoPane.html]
santiagojrodriguez
  • Observer

  • 13 posts
  • Corona SDK

Yes, that does the trick.

 

I was thinking first that maybe I had some way to implement code that would only run once per installation. Probably because I was under the impression that it could be troublesome to verify file existence on every run but if that's not the case your suggestion solves my problem perfectly.

 

Thanks again (and thanks Rob).




[topic_controls]
[/topic_controls]