How to persist data in an Electron app?

前端 未结 9 1139
刺人心
刺人心 2021-01-30 07:00

I\'ve been scouring the Electron documentation to try and figure out how to persist data in an Electron app. For example, in iOS or OS X, you could use NSUserDefaults to store u

9条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-30 07:21

    You can go for Indexeddb, which is most likely suitable for client-side app needs due to:

    • Its built-in versioning mechanism. Client-side applications often face version fragmentation as users don't usually update to new version at the same time. So checking version of existing database and update accordingly is a good idea.
    • It's schemaless, which allows flexibility of adding more data to client's storage (which happen pretty often in my experience) without having to update database to a new version, unless you create new indices.
    • It support wide ranges of data: basic types as well as blob data (file, images)

    All in all it's a good choice. The only caveat is that chromium cores may automatically wipe out indexeddb to reclaim disk space when storage is under strain if navigator.storage.persist is not set, or when the host machine is crashed leaving indexeddb in corrupted state.

提交回复
热议问题