Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Local storage, session storage and global storage

Modern browsers like Apple Safari, Mozilla Firefox and Google Chrome supports application centric storage, meaning that you can store data in the browser cache. There are 3 variations you can choose from: local storage, session storage and global storage. In many ways the API resembles a lightweight variation of TRegistry under Delphi or an INI file section.

Local storage

When used, the browser creates a persistent isolated storage file (a cabinet file) that is private for your application alone (the scope of what “your application” is – is defined by the domain and URI). No other website can access this data.

Session storage

If you use a session storage object the browser creates, much like local storage, a private storage file for your application. But this file is temporary and is deleted from the file-system when the browser is closed or the user navigates somewhere else.

Global storage

Global storage creates a storage space that can be accessed by any other application. This is used by websites like facebook and youtube (and most other big labels). The global data is in turn often picked up by third party websites which can then use the login information to identify you.

Using storage from Smart Mobile studio

Smart supports all 3 types of storage (and more) mechanisms and have isolated these classes in the unit w3storage. The classes all derive from a common ancestor making it a snap to change storage medium as you move along.

Unless you want to isolate the storage object in a custom unit, the most logical place to initialize the object is in your TApplication object. Under Delphi TApplication was quite anonymous, but under Smart this is slightly different and TApplication really is the central hub of your projects.

procedure TApplication.ApplicationStarting;
var
  mForm: TW3CustomForm;
begin
  FStorage := TW3LocalStorage.Create;
  FStorage.Open('TestApp');
  //Add code above this line
  mForm := TForm1.Create(Display.View);
  mForm.Name := 'Form1';
  RegisterFormInstance(mForm,True);
  inherited;
end;
Reading and writing information

Once the storage object is initialized and made public as part of your TApplication instance, reading information is a piece of cake:

procedure TForm1.InitializeObject;
begin
  inherited;
  FEdit := TW3EditBox.Create(self);
  FEdit.onChanged := HandleEditChanged;
  try
    FEdit.Text := TApplication(Application).Storage.getKeyStr('myText');
  except
    on e: exception do;
  end;
end;

Writing information is likewise simple:

procedure TForm1.HandleEditChanged(sender:TObject);
begin
  TApplication(Application).Storage.setKeyStr('myText',FEdit.Text);
end;

As you probably notice, the storage medium is string only. This is not something imposed by us – but the way the javascript API is defined. You can store as many key-value pairs as you like (with a limit of roughly 10 megabytes).

External references
  • http://dev.w3.org/html5/webstorage/#the-storage-interface
  • http://www.w3.org/TR/html5/offline.html

Leave a comment Cancel reply

You must be logged in to post a comment.

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS