(SOLVED) Local Storage is erased upon application restart

Hi fusetools team. I stored a token with File System module and it is working fine and I am able to retrieve it anywhere in the app, until I restart the application and it is erased and returns value null. Here is the code with which I used to save the token:

var token = Observable();
var path = FileSystem.dataDirectory + "/" + "localStorage.json";

FileSystem.readTextFromFile(path).then(function(content) {
    token.value = data.token;
}).catch(function(error) {
  console.log("Unable to read file due to error:" + error);
});```

I tried Local Storage module too, and it did the same the token is saved until the restart of the app. Could you help save the token permanently

dataDirectory is intended to be a location where the data can be persistent. There are some situations where installing an app for development might accidentally delete that directory. During normal app updates it is intended to be persistent however.

Your sample only shows how you load the token. What code are you using to save it?

Thank you all for your fast reply. The code where it is saved is:

var path = FileSystem.dataDirectory + "/" + "localStorage.json";
var payload = {
    token: token.value
  }

  FileSystem.writeTextToFile(path, JSON.stringify(payload))
    .then(function() {
        return FileSystem.readTextFromFile(path);
    })
    .then(function(text) {
        console.log("The read file content was: " + text);
        saveLock = false;
    })
    .catch(function(error) {
        console.log("Unable to read file due to error:" + error);
        saveLock = false;
  });

I wonder if perhaps the file isn’t yet closed in the first callback handler. That is, it isn’t ready to be read yet by the time it gets to the that readTextFromFile. Try adding a callback via setTimeout to see if it can be read at a slightly later time. This would help us verify where the defect might be coming from.

Are you sure the error isn’t from the writing, and not the reading? Verify that you do indeed get to that readTextFromFile code.

You could also test with the “documents” directory on the devices, since that is guaranteed to be saved (though it won’t work in Preview)

var saveDir = FileSystem.dataDirectory
if (Environment.ios) {
	saveDir = FileSystem.iosPaths.documents
} else if (Environment.android) {
	saveDir = FileSystem.androidPaths.files
}
var savePath = saveDir + "/state"

I did figure out the problem and it doesn’t concern the writing and reading from the file. I assigned a value to the observable token and saved it is content to a file. On value change of token I rewrote on the file the token value. On the restart of the application the value of the observable is null , thus it is saved to the value null. Thank you and sorry for the inconvenience.