-
Notifications
You must be signed in to change notification settings - Fork 84
LocalStorage
This sdk documentation is deprecated and will not be updated. Check out our new docs at https://sdk.buildfire.com/docs/local-storage/
LocalStorage works in isolated mode for plugins so that plugin instances don't share the same localStorage. This behavior is applied on the standard window.localStorage or using buildfire.localStorage.
LocalStorage is limited to 2k (2048) characters in length for the plugin instance as a whole. If more storage is required consider using https://github.com/BuildFire/sdk/wiki/File-System-Services.
The following are the supported localStorage functions:
window.localStorage.getItem
window.localStorage.setItem
window.localStorage.removeItem
window.localStorage.clear
Please note that using indexed syntax
[]is not supported and is not persisted in iOS.setItemandgetItemshould be used instead.
It is recommended to use buildfire.localStorage instead of standard window.localStorage to have access to errors and to avoid browser incompatibilities in the future.
Acting very similar to window.localStorage. However, called synchronously with a callback and a return value when applicable.
Example:
buildfire.localStorage.setItem("TEST",{a:"123"}, function(e,r){
console.log("item added");
buildfire.localStorage.getItem("TEST", function(e,r) {
console.log("item retrieved", JSON.stringify(r) );
buildfire.localStorage.removeItem("TEST", function(e,r) {
console.log("item removed");
});
});
});
This method will tell the app to save the value under this key and does not return a value.
- key (string): this is the unique identifier for your data. Make sure this is unique since other plugins are sharing the same storage there is potential overrides.
- value (string): this is the data you want to save. If a non-string is passed in a stringification will occur and when the item is retrieved again you it will be in string format. To return to its non-string type you will need to parse it. (see parseInt, parseFloat, JSON.parse ...etc)
- callback (function) : this is the function that will be called with the operation is completed.
Parameters
- error: if there is an error this will be set with the error object
- result: will return
trueif the save occurred successfully
buildfire.localStorage.setItem("myName","Daniel Hindi", function(error,result){
if(error)
console.error("something went wrong!", error);
else
console.log("All is well, data saved and other plugins can now access it");
});
This method will retrieve the previously saved value under this key and returns the value as a function return and inside the callback.
- key (string): this is the unique identifier for your data.
- callback (function) : this is the function that will be called with the operation is completed.
Parameters
- error: if there is an error this will be set with the error object
- value: will return a string or undefined if not found.
buildfire.localStorage.getItem("myName", function(error,value){
if(error)
console.error("something went wrong!", error);
else if (value)
console.log("name: " , value);
else
console.log("nothing was previously saved");
});
This method will remove the previously saved value under this key and does not return a value.
- key (string): this is the unique identifier for your data.
- callback (function) : this is the function that will be called with the operation is completed.
Parameters
- error: if there is an error this will be set with the error object
- value: will return true if all went well.
buildfire.localStorage.removeItem("myName", function(error,result){
if(error)
console.error("something went wrong!", error);
else
console.log("value removed");
});
This method will remove all plugin's localStorage keys and values. It does not return any value.
- callback (function) : this is the function that will be called with the operation is completed.
Parameters
- error: if there is an error this will be set with the error object
- value: will return true if all went well.
buildfire.localStorage.clear(function(error,result){
if(error)
console.error("something went wrong!", error);
else
console.log("localStorage cleared");
});