- Home /
Synchronous vs asynchronous file reading/writing
We're currently serializing and saving data for our game to a series of json files during play and also loading and deserializing at start-up/loading. The game is for PC & OSX, and we're using the PersistentDataPath as the location for these files.
We perform these actions as synchronous functions, which can cause the game to lock up during play as it executes these read/write functions. We've had a couple of bug reports around corrupted json files and, though we ensure that the save data is secure by creating a backup before writing to them, it would be good to know what the cause is.
I'm wondering if the application freeze during this synchronous read/write could possibly be the cause? I've heard that file read/writes should be performed as asynchronous tasks on certain platforms and I'm wondering if going async might eliminate this issue for us.
What have people'e experiences been with file reading/writing and synchronous functions?
You need to implement a buffer if you haven't already. Reading and writing should be a Factory-Line like process, handled almost independently and non-aggressively to the rest of the program. Dont ask for a big chunk when you need it.
Ask for little chunk, little chunk, little chunk, little chunk, now I need it, oh what do you know I already have it.
The techniques are not dissimilar to handshaking in an ASync rs232 port. The buffers are contantly flowing and you need to check if its safe to write or ready to read.
Answer by Paulius-Liekis · Jul 22, 2015 at 12:00 PM
You're more likely to have more issues with async approach, because of all the potential threading issues. And it won't solve your problem. The app blocks when synchronously reading a file, because... it's reading a file from disk :) That's what synchronous read does - it that area of the disk is not cached, then it will have to physically spin the disk and read that file, and that takes time. So the blocking time mostly depends on what's in the cache.
Anyway your problem is most likely related to these issues:
User kill the app during writing of the file
You do not close file handles properly (and data doesn't get flushed)
Some other bug in reading / writing
F-up files on user harddrive (which is not very likely, but possible)
Your answer
Follow this Question
Related Questions
file saved to iOS persistent directory exists during session but is gone next session 0 Answers
Opening data file from asset folder 2 Answers
How to set persistent data path to sdcard on android 0 Answers
Empty Application.persisentDataPath on Android device causing UnauthorizedAccessException 1 Answer
Where to save games on desktop? 0 Answers