Skip to content

LocalStorage

o5faruk edited this page May 2, 2021 · 11 revisions

⚠️⚠️⚠️

This sdk documentation is deprecated and will not be updated. Check out our new docs at https://sdk.buildfire.com/docs/local-storage/

⚠️⚠️⚠️

Shared 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.

Standard window.localStorage

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. setItem and getItem should 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.

buildfire.localStorage

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");
                });
            });
        });

buildfire.localStorage.setItem(key,value, callback)

This method will tell the app to save the value under this key and does not return a value.

Parameters

  • 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 true if the save occurred successfully

Example

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");
});

buildfire.localStorage.getItem(key,callback)

This method will retrieve the previously saved value under this key and returns the value as a function return and inside the callback.

Parameters

  • 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.

Example

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");
});

buildfire.localStorage.removeItem(key,callback)

This method will remove the previously saved value under this key and does not return a value.

Parameters

  • 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.

Example

buildfire.localStorage.removeItem("myName", function(error,result){
     if(error)
          console.error("something went wrong!", error);   
     else
          console.log("value removed");
});

buildfire.localStorage.clear(callback)

This method will remove all plugin's localStorage keys and values. It does not return any value.

Parameters

  • 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.

Example

buildfire.localStorage.clear(function(error,result){
     if(error)
          console.error("something went wrong!", error);   
     else
          console.log("localStorage cleared");
});

Clone this wiki locally