In a recent project, we have found a need to persist NativeScript app data after an app had closed.
Our requirements included:
- data must be persisted through an app reset, but not after an app update or uninstall/reinstall
- must be cross-platform (iOS and Android)
- should persist a small amount of data
In this post, I’ll walk you through the options we considered and why we decided to use a “simple “file” from the file-system module to persist NativeScript app data after an app has closed.
In our research, we considered several different mechanisms for persisting data, including:
- Nathaniel Anderson’s nativescript-sqlite module
- NativeScript’s application-settings module
- NativeScript’s file-system module
Nathaniel’s nativescript-sqlite module is extremely robust and is everything you really need to persist complex data in a normalized SQL-like form within NativeScript apps. With the module, you can either create a database persistsed to the local file system of your app at runtime, or you can ship a database with your app. This module fulfills all of our requirements, and was a strong consideration. We eventually decided not to leverage this module, as the amount of data we were storing is limited, and we felt using this module was too much for what we were trying to accomplish.
The NativeScript application-settings module is extremely easy to use and is intended for storing small amounts of data within a NativeScript app. However, data stored within application settings is not persistsed when an aoplicaiton is closed. As a result, the application-settings module was not a viable option.
The final option we considered was NativeScript’s file-system module. The file-system module provided us all of the functionality we needed, and allowed us a simple way to persist NativeScript app data after an app has closed.
I hope this example helped you understand several options you have available to persist NativeScript app data after an app has closed. If you have any questions or comment, please let me know. Thanks!