Brosteins

Developers, Technology Evangelists, Bros.

Persist NativeScript App Data After an App has Closed

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
  • should be simple (not time consuming) for developers to persist complex JavaScript objects

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.

Options

In our research, we considered several different mechanisms for persisting data, including:

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.

Code Example

To use the file-system model, you require the “file-system” module within your NativeScript app’s JavaScript code. Once a reference is obtained, you can can create or open a file form the local file system by using the fileSystemModule.knownFolders.documents().getFile(fileName) command. Once you have a reference to the file, you can read, write, and clear data from the file. In the code snippet below, you can see how easy it is to write, read, and clear data from a file.

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!

Share

10 comments for “Persist NativeScript App Data After an App has Closed

  1. Avatar
    February 1, 2016 at 10:38 am

    Thanks for sharing.
    What about : https://www.npmjs.com/package/nativescript-loki
    It’s fast, and, i suppose it will be your solution.

  2. Avatar
    Juanillo
    February 18, 2016 at 12:19 am

    Ok, so the question now is, how do we persist data after app new update or even after uninstall reinstall?
    Great article!

    • Avatar
      March 2, 2016 at 10:32 am

      Thanks for the great question, Juanillo! For the uninstall/reinstall scenario, you should not persist data. The purpose of an uninstall is to remove the app and all associated data from the mobile device. I’m unsure about the app “updates”. Let me check with a few people and I’ll let you know.

  3. Avatar
    Nick Brown
    October 11, 2016 at 11:14 am

    Sorry if this is a bit of a dumb question, but do I need to create the ‘persistedData.json’ file? If so, where do you put it?

    • Avatar
      Nick Brown
      October 13, 2016 at 1:24 pm

      Hey, figured out the dumb question, so here’s another one. Half the time that I load a page using data from my persisted file, I need to load the page twice for the XML to update with the correct data. Any idea why?

    • Avatar
      Nick Brown
      October 13, 2016 at 1:25 pm

      Thanks for the amazing article by the way! This was exactly what I needed to save sessions data in my app

  4. Avatar
    Light
    November 5, 2017 at 9:28 am

    thanks for good information
    I need to store custom object that i have created in typescript, how can i store the object and retrieve it
    Thanks in advance

  5. Avatar
    December 24, 2017 at 8:26 am

    You write: “data stored within application settings is not persisted when an application is closed”.

    I disagree.

  6. Avatar
    alexmeia
    January 2, 2018 at 8:42 pm

    However, data stored within application settings is not persisted when an applicaiton is closed.

    Are you sure about that?
    I found no evidence of this in the official docs, and in some test i did it seems that data are persisted when the app is closed and then reopened.

  7. Avatar
    August 2, 2018 at 5:48 pm

    Hello I’ve been presented with these options too, when trying to persist data from the application, specially to be used as cache. For one dimensional data, I use appsettings. For more complex data (datasets), indeed sqlite was a natural choice. The plugin works fine so far but I’ve found that (unless I’m wrong) you cannot pick up the data via association (example: data.fieldname), you have to go for data[0] etc, and this is a no-go when I bring lots of fields in the query result from a remote DB.

    So for now I’m using file system, storing a json stream (the file system implementation doesn’t have data append functionality implemented but I can do ok with a full write). Question is.. what if there are several users in the application? Do you recommend using some key field or a unique filenaming to avoid data clash? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.