Question
I'm developing a Tizen web application where I use JavaScript's localStorage to store and retrieve data. However, I've noticed that whenever I reinstall my application through Tizen Studio, all localStorage data gets cleared.
Is there an alternative data storage method that will persist even after application reinstallation?
Answer
Problem Understanding
The issue occurs because localStorage is designed to be cleared when an application is uninstalled. This is standard behavior across platforms. When you reinstall your application through Tizen Studio, it performs a complete uninstall/reinstall cycle, which clears the localStorage.
Solution Methods
- Use Tizen Filesystem API: The tizen.filesystem API provides persistent storage that survives application reinstallation.
- Implement a localStorage-like wrapper: Create a custom storage solution that uses the filesystem API but mimics localStorage's interface.
- Enable Rapid Development System: This feature in Tizen Studio replaces only changed files during development, avoiding complete reinstallation.
Code Examples
Here's a complete implementation of a localStorage-like wrapper using tizen.filesystem:
// tizen-storage.js
/**
* Default storage directory on Tizen file system
*/
const storageDir = 'documents';
/**
* Uses the tizen.filesystem API to store and get data
*/
export default {
set: (key, value, dir = storageDir) => {
if (!window.tizen || !window.tizen.filesystem) {
localStorage.setItem(key, value);
return;
}
const handle = window.tizen.filesystem.openFile(`${dir}/${key}`, 'w');
handle.writeString(value);
handle.close();
},
get: (key, dir = storageDir) => {
if (!window.tizen || !window.tizen.filesystem) {
return localStorage.getItem(key);
}
let handle;
let content;
try {
handle = window.tizen.filesystem.openFile(`${dir}/${key}`, 'r');
} catch (err) {
return null;
}
content = handle.readString();
handle.close();
return content;
}
}
Example usage:
import tizenStorage from './tizen-storage';
export const updateHasPlayed = (url, currentTime) => dispatch => {
const key = 'hasPlayed';
const currentHasPlayed = tizenStorage.get(key);
const json = currentHasPlayed === null
? {}
: JSON.parse(currentHasPlayed);
json[url] = currentTime;
tizenStorage.set(key, JSON.stringify(json));
}
Additional Tips
- The filesystem API requires appropriate privileges in your config.xml:
<tizen:privilege name="http://tizen.org/privilege/filesystem.read"/> <tizen:privilege name="http://tizen.org/privilege/filesystem.write"/> - For development purposes, consider enabling Rapid Development System in Tizen Studio to avoid frequent data loss during testing.
- Remember that filesystem operations are asynchronous in nature, so consider error handling for production applications.