Json files cannot not be updated when it is downloaded online

Hi All, firstly apologize my poor English.

My app is designed to download the new index online everytime it is opened.

However, even i updated the index content of index.json online, its content in corona are still not updated.

e.g. _G.index[3] below supposed to be “Geography” if it downloaded the new index.json, but it is still got "CHGeography"which is the 3rd value of index.json before.

I suspect the old index.json remains there, so Corona SDK is actually did not really downloaded the new one.

Any idea how to delete the caches? 

local function networkListener( event ) --local function uploadListener( event ) if ( event.isError ) then print( "Network Error." ) else if ( event.phase == "began" ) then print( "Upload started" ) elseif ( event.phase == "progress" ) then print( "Uploading... bytes transferred ", event.bytesTransferred ) elseif ( event.phase == "ended" ) then print( "Upload ended..." ) print( "Status:", event.status ) print( "Response:", event.response ) \_G.index = loadsave.loadTable("index.json", system.DocumentsDirectory) print("here") print(\_G.index[3]) end end end local headers = {} headers["Content-Type"] = "application/json" local params = {} params.headers = headers --[[params.body = { filename = "temp.json", baseDirectory = system.DocumentsDirectory }]] local function downloadjson () network.download( "http://mcno.orgfree.com/uploads/index.json", "GET", networkListener, params, "index.json", system.DocumentsDirectory ) end os.remove( system.pathForFile( "index.json", system.DocumentsDirectory ) ) local lfs = require( "lfs" ) -- Get raw path to the app documents directory local doc\_path = system.pathForFile( "", system.DocumentsDirectory ) for file in lfs.dir( doc\_path ) do     -- "file" is the current file or directory name     print( "Found file: " .. file ) end downloadjson()

You could delete the existing one and then download the updated version.

I did delete the files in system first, and checked to ensure it is not there before the download, but still no luck.

updated the code above, any other ideas?

os.remove( system.pathForFile( "index.json", system.DocumentsDirectory ) ) local lfs = require( "lfs" ) -- Get raw path to the app documents directory local doc\_path = system.pathForFile( "", system.DocumentsDirectory ) for file in lfs.dir( doc\_path ) do     -- "file" is the current file or directory name     print( "Found file: " .. file ) end

Caching has nothing to do with the local copy. It has to do with what when it thinks the URL was accessed last. It basically says “I last went to this URL at 10 am. The server says the file was last updated at 9am. Therefore I have the latest file. There is no reason to download it again”. It’s not paying any attention to if you deleted the file or not.

You’ve got a couple of things you can do. There are probably some headers you can pass that will turn off caching. But the common practice is to make the URL unique.

local function downloadjson () network.download( "http://mcno.orgfree.com/uploads/index.json?cb=" .. tostring(os.time()), "GET", networkListener, params, "index.json", system.DocumentsDirectory ) end

Now the URL will be unique every time it’s called (assuming it’s not faster than once per second) and you should get the latest file.  The ? says to add a key-value pair. The key is “cb” for cache bust, but you can make up anything you don’t think the server would use. Then using the number of seconds since epoch will pretty much guarantee a new value on every run making your URL unique. Since you’re not hitting a service that would look for a key named “cb”, it should work for you.

Rob

Thanks Rob, it did the tricks!   :)  :)  :slight_smile:

You could delete the existing one and then download the updated version.

I did delete the files in system first, and checked to ensure it is not there before the download, but still no luck.

updated the code above, any other ideas?

os.remove( system.pathForFile( "index.json", system.DocumentsDirectory ) ) local lfs = require( "lfs" ) -- Get raw path to the app documents directory local doc\_path = system.pathForFile( "", system.DocumentsDirectory ) for file in lfs.dir( doc\_path ) do     -- "file" is the current file or directory name     print( "Found file: " .. file ) end

Caching has nothing to do with the local copy. It has to do with what when it thinks the URL was accessed last. It basically says “I last went to this URL at 10 am. The server says the file was last updated at 9am. Therefore I have the latest file. There is no reason to download it again”. It’s not paying any attention to if you deleted the file or not.

You’ve got a couple of things you can do. There are probably some headers you can pass that will turn off caching. But the common practice is to make the URL unique.

local function downloadjson () network.download( "http://mcno.orgfree.com/uploads/index.json?cb=" .. tostring(os.time()), "GET", networkListener, params, "index.json", system.DocumentsDirectory ) end

Now the URL will be unique every time it’s called (assuming it’s not faster than once per second) and you should get the latest file.  The ? says to add a key-value pair. The key is “cb” for cache bust, but you can make up anything you don’t think the server would use. Then using the number of seconds since epoch will pretty much guarantee a new value on every run making your URL unique. Since you’re not hitting a service that would look for a key named “cb”, it should work for you.

Rob

Thanks Rob, it did the tricks!   :)  :)  :slight_smile: