Search Unity

Implementing a robust and auto-save system for user content?

Discussion in 'iOS and tvOS' started by Noisecrime, Mar 2, 2016.

  1. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Hi,

    I have a creative based app where users design 3D models and have been trying to find information as to the best practices in terms of creating a robust file save system and auto-save feature. Unfortunately so far i've not found any useful information so I thought i'd ask directly to see if anyone else has faced this challenge?

    Each project will need to save at least two files, a thumbnail png and xml file, from tests these together will average around 200KB.

    As far as I can tell there are two issues to address, firstly that in general users expect apps to auto-save their data and it is often frowned upon to force a user to click a save button, secondly that for robustness you can't simply overwrite existing files in case of corruption.

    Now implementing auto-save is simple enough, there are presumably key points in the apps run cycle where it makes sense to make an auto-save, for example if it enters the background ( application.pause ). If the user closes the active project to open or creating a new one. What i'm less sure about is whether I should make periodic saves whilst the app is running, mainly as i'm concerned about potential delays or lag this might introduce to the user experience.

    As for robustness that should be addressable by having some incremental versioning for auto-saves. So you are never overwriting data and always have a couple of previous states to return to should a file become corrupted. My current thinking here is for the open project;
    • Perform periodic saves to special folder with a timestamp naming convention to identify latest version.
    • After each save if there are more than n files in special folder delete the oldest.
    • On specific save events ( close project, application.pause ) save to special folder, move to projects folder, validate, delete last version from projects folder.
    • On app restart, check special folder to find latest version and which project, compare to version in Project folder and copy over if its newer.

    This seems pretty good, maybe needs a little tweaking, it removes potential costly operations of validating to specific save events and not from general auto-saving. It does however mean i'm having to just trust that periodic saves are not being corrupted and that having several versions will at least provide some recoverability. I'm concerned over the amount of time both periodic saves and specific save events might take, especially the specific saves since these will often happen when the app goes into the background.

    What i'm really unsure of here is whether the OS will provide sufficient processing time/resources to make these specific saves, for example when user switches app, or if user powers down device?

    An obvious method to address this problem would be asynchronous saves, but I've not found anything online for getting Unity to do this, other than I guess writing a file in parts over a stream and i'm somewhat concerned this would introduce more complexity into an already somewhat complex system.

    Some specifics - each user project will need to save a large thumbnail ( 256x256 or even 512x512 ) png or jpg file along with an xml file ( hopefully as binary data not text). The thumbnail as 512x512 png ranges from 50KB to 400KB and averages about 140KB, whilst the xml ranges from 20KB to 250KB and averages around 40KB.

    So any thoughts? Anyone had experience of this and knows of any pitfalls or issues to be aware of?