Storing initial data in a JSON-file, loading it and saving it.

Hi all,

I’ve been wasting a couple of hours trying to figure out why my Corona Simulator and IOS Simulator both were able to load a JSON file stored in a subdirectory, but why my iPhone / iPad both failed loading the file.

Somehow the app is unable to locate the file in the Documents folder (system.DocumentsDirectory).
I still don’t know why it won’t work, but I made a workaround by putting the data in a table, encoding it, and writing it to the Documents directory if the file doesn’t exist yet. This is a stupid workaround, but it involved just two values.

However, can somebody explain why the device app is unable to locate the file in the DocumentsDirectory?
I physically created a “Documents”-folder in the root directory, added one in the sandbox folder and tried different sub-folders, to no avail.

The annoying thing is that it’s hard to debug, because even after setting the vbuffer to “no” I’m still not getting any debug information in XCode or in the console.

Any help would be appreciated. [import]uid: 86582 topic_id: 29247 reply_id: 329247[/import]

Sounds familiar to anyone? Sorry for asking again, it kinda bugs me as my other app in development fully relies on JSON data too (with much more data). [import]uid: 86582 topic_id: 29247 reply_id: 117704[/import]

Hi ferdiwillemse

See this thread for what I think will be a helpful explanation for you: http://developer.coronalabs.com/forum/2012/07/30/problem-reading-text-files.

To elaborate on what I wrote there, the folder in which your project is stored (i.e., all of your code, graphics, sounds, and other assets) is the system.ResourceDirectory. This directory is read-only. Any subdirectories, whether you name them “Documents” or anything else, are still part of the system.ResourceDirectory.

The system.DocumentsDirectory is a directory where you can save files that should persist between runs of your application. Importantly, when your application is first installed, it will be empty. The only way to get files there on an actual device is when your app actually runs. Your app could simply write new files there from code (which it sounds like you’ve done), or it could copy versions that you’ve pre-created and included in your system.ResourceDirectory (see http://developer.coronalabs.com/node/2937).

If you place files in the system.DocumentsDirectory on your computer manually, as you did, you’ll get it to work in the Simulator, but since that directory isn’t part of the app build, it won’t work on a device.

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 29247 reply_id: 117707[/import]

Hi Andew,

Thank you for your clear explanation. I knew the ResourceDirectory is read-only, but somehow my app refused to read the json file there too. It worked in the simulator though. I don’t know , I’ve been hunting that (what I thought) bug for hours and I probably just needed some sleep :slight_smile:

It’s good to hear that my approach of writing the new file from the app is the right approach. From now on I’ll store the data-file prototypes in the ResourceDirectory (or subdirectory since I like structured folders :slight_smile: and copy them over to the DocumentsDirectory on first initialization of the app.

Thanks again! That was really helpful!

  • Ferdi [import]uid: 86582 topic_id: 29247 reply_id: 117710[/import]

Mac OS-X is a case-insensitive file system. That is:

Data.json

and

data.JSON

will be successfully accessed as:

dAtA.jSoN

However, iOS is case-sensitive and if the file name in the file system, doesn’t match the file name in your code, it will fail to open.

This is the #1 cause of these kinds of problems. [import]uid: 19626 topic_id: 29247 reply_id: 117725[/import]

Thanks for the heads up!
I have been testing and developing it all on Mac OSX with the correct upper-/lowercase naming. [import]uid: 86582 topic_id: 29247 reply_id: 117730[/import]

Yes that is also what I do in my game.

Copy data file from resource directory to documents directory, then load it.

[import]uid: 84637 topic_id: 29247 reply_id: 117736[/import]