Intermittent problem with localstorage

0 favourites
  • 9 posts
  • Hi,

    I've noticed that on occasion, specifically when I've left my game in the background for a while then return to it, data I've written to local storage isn't restored.

    I'm pretty sure my code's correct - when one of the variables I store in local storage changes, I call a "save" function that writes all the variables I want to save to a dictionary, then saves that dictionary to local storage. When the game starts, I read the dictionary back from local storage, parse it, then update all the variables ready for the game to start.

    If I just put the game into the background, then resume, all is fine (the game's still running, all the variables have their correct value and nothing is read back from local storage).

    If I force quit the app and restart it, all is fine too. The game restarts and loads all the values correctly.

    If I put the app into the background, then start load of other apps, come back to my game it restarts (as opposed to resuming) - this also works without issue.

    It's almost as if iOS is caching the storage but if the app is left in the background for too long, it never actually gets written. I thought the last two scenarios above would replicate the issue, but they don't - it seems to only happen when the app has been in the background for a long time AND several apps have been opened in the mean time (presumably eating up all the RAM forcing my game to shutdown without saving the local storage).

    This is on a 6th Gen iPad with iOS 17.5.1

  • You probably answered your own question, sounds like the app crashed so nothing was saved.

  • You probably answered your own question, sounds like the app crashed so nothing was saved.

    I've just done a test on Windows (WebView2) - I figured out that local storage is written to this file:

    C:\Users\<username>\AppData\Local\<game name>\EBWebView\Default\IndexedDB\https_app.localhost_0.indexeddb.leveldb\000003.log

    And that file is updated as soon as I call: LocalStorage - Set Item

    So even if the app crashed, the data's already written to disk.

    Maybe iOS handles it differently? Maybe if the app crashes, the OS rolls back the local storage to a previous version? (It looks like that 000003.log file contains all previous entries, so in theory the OS could roll back).

  • My priority would be on making it not crash and lower the memory usage rather than trying to work out what it will do when it crashes.

  • How would I go about troubleshooting a crash (if in indeed that's what's happening) that only happens intermittently if the game has been in the background for a long time and other apps have have been opened (that presumably use up all the OS's RAM)?

    Can Xcode help with this?

    I wish I understood more about how Construct/iOS/Webview works with local storage...

  • I'm pretty sure my code's correct - when one of the variables I store in local storage changes, I call a "save" function

    I would check how often this save function is called. Can it be called when the app is resumed from the background? If this is the case and the app crashes at the same time, this definitely can corrupt the data.

    Make sure that you are not writing to LS too often.

  • I would check how often this save function is called. Can it be called when the app is resumed from the background? If this is the case and the app crashes at the same time, this definitely can corrupt the data.

    Make sure that you are not writing to LS too often.

    Oh - this might be really helpful. I DO write often - every second when a match (basketball/pinball hybrid) is in progress. This was an anti-cheat measure - before, if I was losing a game, I could force quit the app, restart and play that match again. I’ll increase the time between writes a little and see if that helps. Thanks!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Try to write to LS only after an event which can't happen on its own when the game is resumed. For example, only after the ball is kicked.

  • In terms of anti-cheat mechanics, you could save to LS once when the match is started, with a score of zero or -1 or whatever, then only update that score if a match is completed. That way you can handle a default loss/forfeit unless otherwise specified.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)