Is there a difference in ios and android file handling?

Hi,

I’m working on an app, which has to download images to the documents folder and overwrite already downloaded ones. I tried it on Windows and Mac simulator (both version 2016.2906), an ios and an android device.

Windows and android: There isn’t any problem, it downloads the image, overwrites previous version and shows the new one in the app right away.

Mac and ios: Here are the problems. Even in the simulator it downloads the files, but once there is a version downloaded, that version stucks. That means even if I delete everything in the documents folder, somehow I can’t download a new version from that file. So a file is downloaded, and the creation date is ok, but the content is the old one, and that shows up in the app. I also checked the image in a web browser from the mac, and it’s the correct one.

I’m using the same code for every version, but it works differently so I assume there’s a difference in file handling. Am I correct? Is there a kind of cache on Mac and ios?

Zsolt

If you download and open a file (especially an image file), it locks the file.  Then you can’t write over it.

Additionally, if you open an image file with the name X, replace that file, then open it again, if X is still in memory you will get the old X.

It you are downloading temporary (to be removed soon) files, do the following:

  1. Download them to the temporary folder.  The (mobile) OS will automatically remove these if you start to run low on space.

  2. Use unique names for all of the files. If you need to use several files to replace/refresh the same content in your game, use an indexing system to track the current name of the source file.

-Ed

Thanks Ed!

Actually the file isn’t locked. If I lock it from Finder on Mac, there is a download error. But apart from that, it should work, the modification date changes if it isn’t locked.

What’s interesting is that if I lock the file I try to overwrite (by setting the downloading file’s name the same for every download), a download-(uniqueID) file is generated. This indicates a failed download: it downloads the file, but can’t overwrite with the name it’s given. So these are unique file names, but it doesn’t matter. If I download from the same url, the content never changes, even if the file changes behind the url.

An example: I download a file with a size of 22 kb from http://mydomain:port/myfile.txt to the documents folder with the name mylocalfile.txt. After a succesful download, I lock the file from Finder. If I now try to download the file with the name mylocalfile.txt, Corona creates a download-something file, every one of them is 22 kb. Now if I change the file on http://mydomain:port/myfile.txt to a size of 98 kb, and try to download again with Corona, an other 22 kb sized download-something file is created. Even if I download it as mylocalfile2.txt it will be a 22 kb mylocalfile2.txt.

This is the case with every try. Unless I copy the 98 kb myfile.txt to http://mydomain:port/myfile2.txt and download this as mylocalfile2.txt. Now mylocalfile2.txt becomes an 98 kb file. Finally. So I can overwrite files, if I download them from another url every time I want to overwite them.

Oh and I forgot to mention that I never opened these files in Corona, just downloaded them, so it shouldn’t be in the memory or locked. And it persists after restarting the Corona Simulator and deleting everything from the documents folder of the app.

Is this a bug? I’m using network.download.

Sorry for the long post, I hope it’s clear.

Try this:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/09/remoteFiles.zip

https://www.youtube.com/watch?v=DBjfUR68hLQ&feature=youtu.be

Thanks again!

I’ll definitely try this out (I’m not at home right now), but looking at the code, it looks very similar to what I was doing. What happens if you overwrite the http://roaminggamer.com/wp-content/uploads/2014/05/eat_lean_plugins.png file with a modified one for example? In my experience, the new version of the file will never be downloaded, unless you upload it with a new name: http://roaminggamer.com/wp-content/uploads/2014/05/eat_lean_plugins_mod.png

This is strange, because on Windows and android, this isn’t the case.

Sounds like on iOS & OSX that the file is being cached. When you request the download, it just gets the file from the cache.

A solution posted by Omnigeek Media seems to be what you need.

"The easiest solution is to add a string to the end of the URL to make the URL unique:

url = “http://mysite.com/images/pic.png?a=” … os.time()

It will trick the system since it thinks it’s a new URL and the server will ignore the query string added on. If your URL already uses query strings, then just tack an “&a=” … os.time() on to the end. If you’re query string is already passing a variable called a, just make up something not in use."

https://forums.coronalabs.com/topic/27167-networkdownload-downlading-from-cachedb/

Thanks nick_sherman! This seems to be the solution, I’ll test it out as soon as I can. I couldn’t find this old topic.

@nick_sherman (and by proxy Rob) - Nice trick.

@gergely.zsolt89

I created a modified version of the example only writing to two files (alternatingly):

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/09/remoteFiles2.zip

  • image1.png
  • image2.png

This works fine client side.

I don’t however have a way to modify my server files (especially since they are live and part of this page. )

Thanks guys! This trick works great! What a dumb thing from Apple. This should be in the documentation.

I’m grateful to you!

If you download and open a file (especially an image file), it locks the file.  Then you can’t write over it.

Additionally, if you open an image file with the name X, replace that file, then open it again, if X is still in memory you will get the old X.

It you are downloading temporary (to be removed soon) files, do the following:

  1. Download them to the temporary folder.  The (mobile) OS will automatically remove these if you start to run low on space.

  2. Use unique names for all of the files. If you need to use several files to replace/refresh the same content in your game, use an indexing system to track the current name of the source file.

-Ed

Thanks Ed!

Actually the file isn’t locked. If I lock it from Finder on Mac, there is a download error. But apart from that, it should work, the modification date changes if it isn’t locked.

What’s interesting is that if I lock the file I try to overwrite (by setting the downloading file’s name the same for every download), a download-(uniqueID) file is generated. This indicates a failed download: it downloads the file, but can’t overwrite with the name it’s given. So these are unique file names, but it doesn’t matter. If I download from the same url, the content never changes, even if the file changes behind the url.

An example: I download a file with a size of 22 kb from http://mydomain:port/myfile.txt to the documents folder with the name mylocalfile.txt. After a succesful download, I lock the file from Finder. If I now try to download the file with the name mylocalfile.txt, Corona creates a download-something file, every one of them is 22 kb. Now if I change the file on http://mydomain:port/myfile.txt to a size of 98 kb, and try to download again with Corona, an other 22 kb sized download-something file is created. Even if I download it as mylocalfile2.txt it will be a 22 kb mylocalfile2.txt.

This is the case with every try. Unless I copy the 98 kb myfile.txt to http://mydomain:port/myfile2.txt and download this as mylocalfile2.txt. Now mylocalfile2.txt becomes an 98 kb file. Finally. So I can overwrite files, if I download them from another url every time I want to overwite them.

Oh and I forgot to mention that I never opened these files in Corona, just downloaded them, so it shouldn’t be in the memory or locked. And it persists after restarting the Corona Simulator and deleting everything from the documents folder of the app.

Is this a bug? I’m using network.download.

Sorry for the long post, I hope it’s clear.

Try this:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/09/remoteFiles.zip

https://www.youtube.com/watch?v=DBjfUR68hLQ&feature=youtu.be

Thanks again!

I’ll definitely try this out (I’m not at home right now), but looking at the code, it looks very similar to what I was doing. What happens if you overwrite the http://roaminggamer.com/wp-content/uploads/2014/05/eat_lean_plugins.png file with a modified one for example? In my experience, the new version of the file will never be downloaded, unless you upload it with a new name: http://roaminggamer.com/wp-content/uploads/2014/05/eat_lean_plugins_mod.png

This is strange, because on Windows and android, this isn’t the case.

Sounds like on iOS & OSX that the file is being cached. When you request the download, it just gets the file from the cache.

A solution posted by Omnigeek Media seems to be what you need.

"The easiest solution is to add a string to the end of the URL to make the URL unique:

url = “http://mysite.com/images/pic.png?a=” … os.time()

It will trick the system since it thinks it’s a new URL and the server will ignore the query string added on. If your URL already uses query strings, then just tack an “&a=” … os.time() on to the end. If you’re query string is already passing a variable called a, just make up something not in use."

https://forums.coronalabs.com/topic/27167-networkdownload-downlading-from-cachedb/

Thanks nick_sherman! This seems to be the solution, I’ll test it out as soon as I can. I couldn’t find this old topic.

@nick_sherman (and by proxy Rob) - Nice trick.

@gergely.zsolt89

I created a modified version of the example only writing to two files (alternatingly):

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/09/remoteFiles2.zip

  • image1.png
  • image2.png

This works fine client side.

I don’t however have a way to modify my server files (especially since they are live and part of this page. )

Thanks guys! This trick works great! What a dumb thing from Apple. This should be in the documentation.

I’m grateful to you!