SlideShare uma empresa Scribd logo
1 de 65
Baixar para ler offline
Local Storage
         Ivano Malavolta
    ivano.malavolta@univaq.it
http://www.di.univaq.it/malavolta
Roadmap

•   Introduction
•   Web Storage
•   WebSQL
•   IndexedDB
•   File System Access
•   Final Considerations
Introduction

There are 4 ways to store data locally

• Web storage
   – Local Storage
   – Session Storage
• WebSQL
• Indexed DB
• File System Access
Web Storage

LocalStorage
  stores data in key/value pairs
  persists across browser sessions

SessionStorage
  stores data in key/value pairs
  data is erased when a browser session ends
Introduction

WebSQL Database
 relational DB
 support for tables creation, insert, update, …
 transactional
 persists across browser sessions

Its evolution is called IndexedDB but it is actually
                        IndexedDB,
  not supported by most mobile browsers
Introduction

File System Access
  you can access files locally to your app
  supports main FS operations
   – files creation, move, delete, rename, creation, etc.
  it is not transactional
  persists across browser sessions
Introduction
Mobile browsers compatibility matrix
Roadmap

•   Introduction
•   Web Storage
•   WebSQL
•   IndexedDB
•   File System Access
•   Final Considerations
Web Storage

It is based on a single persistent object called
                      localStorage


You can set values by calling
  window.localStorage.setItem(“name”, “Ivano”);


You can get values back by calling
var name = window.localStorage.getItem(“name”);
Supported Methods
localStorage.key(0)
  Returns the name of the key at the position specified

getItem(“key”)
  Returns the item identified by it's key

setItem(“key”, “value”)
  Saves and item at the key provided

removeItem(“hey”)
  Removes the item identified by it's key

clear()
  Removes all of the key value pairs
Complex Objects

Current implementations support only string-to-string
  mappings

  you can store only strings
  keys can be only strings

You can use JSON serialization if you need to store
  complex data structures
Example of JSON Serialization
// simple class declaration
function Person(name, surname) {
  this.name = name;
  this.surname = surname;
}

// object creation
var user = new Person(‘Ivano’, ‘Malavolta’);

// object serialization
window.localStorage.setItem(“user”, JSON.stringify(user));

// object retrieval
var current =
  JSON.parse(window.localStorage.getItem(“user”));
Checking Existence

You can simply check if the needed element is == null



if (window.localStorage.getItem(“user”)) {
  // there is an object in user
} else {
  // the user key does not have any value
}
Selecting elements
In this case you have to manually iterate on elements

var users = [...]; // array of Person objects
window.localStorage.setItem(“users”,
  JSON.stringify(users));

var allUsers =
  JSON.parse(window.localStorage.getItem(“users”));
var ivanos = [];
for(var i=0; i<allUsers.length; i++) {
  if(allUsers[i].name == ‘Ivano’)
      ivanos.push(allUsers[i]);
}
Counting Elements

Also in this case, we have to do it manually

var usersNumber =
  JSON.parse(window.localStorage.getItem(“users“)).length;
Session Storage

Session Storage provides the same interface as Local
  Storage
     you can call the same methods

but
  Session Storage is cleared between app launches
Roadmap

•   Introduction
•   Web Storage
•   WebSQL
•   IndexedDB
•   File System Access
•   Final Considerations
WebSQL
It provides you a structured SQL relational database

You have to setup a DB schema

You can then perform classical SQL queries

     tx.executeSql("SELECT * FROM User“, [],
      function(tx, result) {
          // callback code
     });
Opening a DB

Done via a dedicated function

var db =
  openDatabase(‘Test', ‘1.0', ‘Test DB', 100000);


It creates a new SQLLite DB and returns a new
  Database object

The Database object will be used to manipulate the data
Opening a DB: syntax
   openDatabase(name, version, displayname, size);


DB name
   the name of the DB

DB version
   the version of the DB

DB Display name
   the display name of the DB

DB Size
   the size of the DB in bytes
Database

It allows to manipulate the data via 2 methods:

changeVersion
  atomically verify the version number and change it

        db.changeVersion("1.0", "1.1");


transaction
  performs a DB transaction
Transactions
It allow you to execute SQL statements against the DB

      db.transaction(queries, error, success);


3 callback functions:

queries : contains the queries to be performed
error : executed if the transaction results in an error
success : executed if the transaction terminates correctly
Transaction Example




http://bit.ly/JlUJde
executeSql
It is the method that performs a SQL statement

The user can build up a database transaction by calling
  the executeSql method multiple times


function populateDB(tx) {
  tx.executeSql('DROP TABLE IF EXISTS USER');
  tx.executeSql('CREATE TABLE IF NOT EXISTS USER (id
      unique, name, surname)');

    tx.executeSql('INSERT INTO USER(id, name, surname)
        VALUES (1, ?, ?)‘, [“Ivano“, “Malavolta“],
        success, error);
}
Result Sets
When the executeSql method is called, it will invoke it's
  callback with a SQLResultSet parameter

It has 3 properties:
insertId
   the ID of the row that has been inserted
rowsAffected
   the number of rows changed by the SQL statement
rows
   the data returned from a SQL select statement
   rows is an object of type SQLResultSetList
Results Sets Example
...
  tx.executeSql('INSERT INTO USER(id, name,surname)
     VALUES (1, ?, ?)‘, [“Ivano“, “Malavolta“],
     success, error);
}

function success(tx, results) {
  var affected = results.rowsAffected(); // 1
}

function error(err) {
  // code for managing the error
}
Result Set Lists
It contains the data returned from a SQL Select statement

length
  the number of rows returned by the SQL query

item(index)
  returns the row at the specified index represented by a
  JavaScript object
Result Set List Example
...
  tx.executeSql(‘SELECT * FROM USER‘, [],
     success, error);
}

function success(tx, results) {
  var size = results.rows.length;
  for (var i=0; i<size; i++){
     console.log(results.rows.item(i).name);
  }
}
Errors

It contains information about an occurred error

code
  A predefined error code
  es. UNKNOWN_ERR, DATABASE_ERR, TOO_LARGE_ERR,
      QUOTA_ERR, SYNTAX_ERR, TIMEOUT_ERR


message
  A description of the error
Error Code Example
...
  tx.executeSql(‘SELECT * FROM USER‘,[],
     success, error);
}



function error(err) {
  console.log(err.code);
}
Roadmap

•   Introduction
•   Web Storage
•   WebSQL
•   IndexedDB
•   File System Access
•   Final Considerations
Indexed DB
It tries to combine Web Storage and WebSQL

You can save data as key/value pairs
                     key/value

You can define multiple DBs

Good Performance
  data is indexed
  asynchronous    it does not block the UI
Indexed DB

An Indexed DB is a collection of object stores

You can drop objects into the stores

You can see stores as a big SQL table with only
  key/value
  key/value pairs

     you don’t need to define a schema upfront
Roadmap

•   Introduction
•   Web Storage
•   WebSQL
•   IndexedDB
•   File System Access
•   Final Considerations
File System Access

It allows you to read, write and navigate file system
  hierarchies

It is fundamental for managing and storing large files
                             client-
  and binary content on the client-side
File System Access Workflow

1.    Request file system access
     – persistent or temporary file system

2.    then you can perform CRUD operations for both
     files and folders:
     –   Create
     –   Read
     –   Update
     –   Delete
Request File System
requestFileSystem(type, size, successCb, [errorCb])


type
   LocalFileSystem.TEMPORARY
   LocalFileSystem .PERSISTENT
size
   size in bytes the app will require for storage.
successCb
   success callback, its argument is a FileSystem object
ErrorCb
   error callback, its argument is a FileError object
Temporary VS Persistent

Temporary
  the files stored by the app can be deleted at the
  browser’s discretion

     no guarantee of persistence

Persistent
  cannot be deleted by the browser without
  authorization by the app
Local File System Example
window.requestFileSystem(
  LocalFileSystem.PERSISTENT,
  0,
  onSuccess,
  onError);

function onSuccess(fileSystem) {
  console.log(fileSystem.name);
}
File System
The FileSystem object has 2 properties:

name
  the name of the file system
  it is unique across the list of exposed file systems

root
  the DirectoryEntry object representing the root
  folder of the file system
Resolving a File URI

resolveLocalFileSystemURI
  retrieve a DirectoryEntry or FileEntry using a URI


window.resolveLocalFileSystemURI(
  "file:///userImg.png", onSuccess, onError);

function onSuccess(fileEntry) {
  console.log(fileEntry.name);
}
Entities

FileEntry
                     The real objects
DirectoryEntry

File    Descriptor

FileReader
FileWriter           Writing & Reading objects
DirectoryReader
File Entry
It represents a file on a file system

isFile (boolean)
   Always true

isDirectory (boolean)
  Always false

name (DOMString)
  the name of the FileEntry, excluding the path

fullPath (DOMString)
  the full absolute path from the root
File Entry Methods
getMetadata(success,
getMetadata(success, fail)
  Look up metadata about a file

moveTo(parentEntry, newName, success, fail)
moveTo( parentEntry, newName,
  Move a file to a different location on the file system

copyTo(parentEntry, newName, success, fail)
copyTo(parentEntry, newName,
  Copy a file to a different location on the file system

toURI()
toURI()
  Return a URI that can be used to locate a file
File Entry Methods
remove(success, fail)
  Delete a file

getParent(success,
getParent(success, fail)
  Look up the parent directory

createWriter(success,
createWriter(success, fail)
  Creates a FileWriter object that can be used to write
  to a file

file(success, fail)
   Creates a File object containing file properties
File
It contains attributes of a single file

name (DOMString)
   The name of the file
fullPath (DOMString)
   The full path of the file including the file name
type (DOMString)
   The mime type of the file
lastModifiedDate (Date)
   The last time the file was modified
size (long)
The size of the file in bytes
Directory Entry
It represents a directory on a file system

It has the same properties of FileEntry
Directory Entry Methods
getMetadata(success,
getMetadata(success, fail)
  Look up metadata about a directory

moveTo(parentEntry, newName,
moveTo(parentEntry , newName, success, fail)
  Move a directory to a different location on the file system

copyTo(parentEntry, newName,
copyTo(parentEntry, newName, success, fail)
  Copy a directory to a different location on the file system

toURI()
toURI()
  Return a URI that can be used to locate a directory
Directory Entry Methods

getParent(success, fail)
getParent(success,
  Look up the parent directory

createReader()
createReader()
  Creates a DirectoryReader object that can be used to read a
  directory

getDirectory(path, options, success, fail)
getDirectory(path,
  Create or look up a directory
  options:
       create    (true | false)
       exclusive    (true | false)
Directory Entry Methods
getFile(path, options, success, fail)
getFile(path,
  Create or look up a file within the directory
  options:
       create    (true | false)
       exclusive    (true | false)

removeRecursively(success, fail)
removeRecursively(success,
  Delete a directory and all of its contents
File Reader
It is used to read the contents of a file

Files can be read as text or as a base64 data encoded
   string

You can also abort() e file reading activity

You can register your own event listeners to receive
  the following events:
  loadstart, progress, load, loadend, error, abort
File Reader Example
entry.file(win, fail);

function win(file) {
   var reader = new FileReader();
   reader.onloadend = function(evt) {
       console.log(evt.target.result);
   };
   reader.readAsText(file);
   // reader.abort();
};

function fail(evt) {
      console.log(error.code);
};
File Writer

It is used to write to a file

The data to be written must be UTF-8 encoded
                               UTF-



You can register your own event listeners to receive
  the following events:

  writestart, progress, write, writeend, error, abort
File Writer
A FileWriter is created for a single file

You can use it to write to a file multiple times
  the FileWriter maintains the file's position and length
  attributes, so you can seek and write anywhere in the file

By default, the FileWriter writes to the beginning of the file
  (will overwrite existing data)

Set the optional append boolean to true in
  the FileWriter's constructor to begin writing to the end
  of the file
File Writer Methods

abort()
  Aborts writing file
seek(byte)
  Moves the file pointer to the byte specified.
truncate(length)
  Shortens the file to the length specified.
write(data)
  Writes data to the file
File Writer Example
entry.createWriter(win, fail);

function win(writer) {
   writer.onwrite = function(evt) {
       console.log(“ok");
   };
   writer.write(“Ivano Malavolta");
};

function fail(evt) {
   // error management
};
Directory Reader

It is an object that lists files and directories in a
  directory

it has only one method:

readEntries(success,
readEntries(success, fail)
  Read the entries of the directory
Directory Reader Example
var directoryReader = dirEntry.createReader();
directoryReader.readEntries(success, fail);

function success(entries) {
  var i;
  for (i=0; i<entries.length; i++) {
      console.log(entries[i].name);
  }
}

function fail(error) {
      console.log(error.code);
}
A Final Example:
     Looking for a file and reading it
window.requestFileSystem(window.PERSISTENT, 0, initFS,
   error);

function initFS(fs) {
  fs.root.getFile(‘log.txt', {}, win, error);
}

function win(fileEntry) {
  fileEntry.file(read, error);
}

function read(file) {
   var reader = new FileReader();
   reader.onloadend = function(e) {
       console.log(this.result);
   };
   reader.readAsText(file);
}
function error(err) { console.log(err);}
Roadmap

•   Introduction
•   Web Storage
•   WebSQL
•   IndexedDB
•   File System Access
•   Final Considerations
Final Considerations
You will likely use all of them in combination

  Use the right API for the right job

Web Storage
• it is not transactional   race conditions
• very simple API, no schema
• only String data     performance issues for complex data
  due to JSON serialization
• session storage will be cleared after the app is closed
• 5Mb quota
Final Considerations

WebSQL
• SQL-based     fast and efficient
• transactional more robust
• asynchronous     does not block the UI
• rigid data structure    data integrity vs agility
• 5Mb quota
Final Considerations

IndexedDB
• simple data model     easy to use
• transactional more robust
• asynchronous     does not block the UI
• good search performance      indexed data
• data is unstructured    integrity problems
• 5Mb quota
Final Considerations

File System
• asynchronous     does not block the UI
• not transactional
• indexing and search are not built-in
     you have to implement your lookup functions
• unlimited storage
   – useful for images, videos, documents, etc.
References



http://docs.phonegap.com/en/1.7.0/

Mais conteúdo relacionado

Mais procurados

Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
Madhuri Kavade
 

Mais procurados (20)

Android Data Persistence
Android Data PersistenceAndroid Data Persistence
Android Data Persistence
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
 
Android Data Persistence
Android Data PersistenceAndroid Data Persistence
Android Data Persistence
 
Entity Framework Database and Code First
Entity Framework Database and Code FirstEntity Framework Database and Code First
Entity Framework Database and Code First
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
Jdbc Java Programming
Jdbc Java ProgrammingJdbc Java Programming
Jdbc Java Programming
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorial
 
Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3
 
Persistences
PersistencesPersistences
Persistences
 
2001: JNDI Its all in the Context
2001:  JNDI Its all in the Context2001:  JNDI Its all in the Context
2001: JNDI Its all in the Context
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 
Android Database
Android DatabaseAndroid Database
Android Database
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
Database Programming
Database ProgrammingDatabase Programming
Database Programming
 
Database in Android
Database in AndroidDatabase in Android
Database in Android
 
Databases in Android Application
Databases in Android ApplicationDatabases in Android Application
Databases in Android Application
 
Techniques for Cross Platform .NET Development
Techniques for Cross Platform .NET DevelopmentTechniques for Cross Platform .NET Development
Techniques for Cross Platform .NET Development
 
Advance Java Practical file
Advance Java Practical fileAdvance Java Practical file
Advance Java Practical file
 
Android Storage - Internal and External Storages
Android Storage - Internal and External StoragesAndroid Storage - Internal and External Storages
Android Storage - Internal and External Storages
 
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
Dicoding Developer Coaching #20: Android | Apa itu Content Provider?
 

Destaque

Destaque (7)

Alla riscoperta di... Cese
Alla riscoperta di... CeseAlla riscoperta di... Cese
Alla riscoperta di... Cese
 
REST - Representational State Transfer
REST - Representational State TransferREST - Representational State Transfer
REST - Representational State Transfer
 
A4WSN: an Architecting environment 4 Wireless Sensor Networks
A4WSN: an Architecting environment 4 Wireless Sensor NetworksA4WSN: an Architecting environment 4 Wireless Sensor Networks
A4WSN: an Architecting environment 4 Wireless Sensor Networks
 
PhoneGap: Local Storage
PhoneGap: Local StoragePhoneGap: Local Storage
PhoneGap: Local Storage
 
Architecture 4 Wireless Sensor Networks
Architecture 4 Wireless Sensor NetworksArchitecture 4 Wireless Sensor Networks
Architecture 4 Wireless Sensor Networks
 
The mobile ecosystem & technological strategies
The mobile ecosystem & technological strategiesThe mobile ecosystem & technological strategies
The mobile ecosystem & technological strategies
 
A Model-Driven Engineering Framework for Architecting and Analysing Wireless ...
A Model-Driven Engineering Framework for Architecting and Analysing Wireless ...A Model-Driven Engineering Framework for Architecting and Analysing Wireless ...
A Model-Driven Engineering Framework for Architecting and Analysing Wireless ...
 

Semelhante a Local Storage

Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
Aren Zomorodian
 
Hibernate jj
Hibernate jjHibernate jj
Hibernate jj
Joe Jacob
 
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File ServerUKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
Marco Gralike
 

Semelhante a Local Storage (20)

Local storage in Web apps
Local storage in Web appsLocal storage in Web apps
Local storage in Web apps
 
[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps[2015/2016] Local data storage for web-based mobile apps
[2015/2016] Local data storage for web-based mobile apps
 
Softshake - Offline applications
Softshake - Offline applicationsSoftshake - Offline applications
Softshake - Offline applications
 
Real World MVC
Real World MVCReal World MVC
Real World MVC
 
La sql
La sqlLa sql
La sql
 
Web Technologies - forms and actions
Web Technologies -  forms and actionsWeb Technologies -  forms and actions
Web Technologies - forms and actions
 
Hibernate
Hibernate Hibernate
Hibernate
 
OOP Adventures with XOOPS
OOP Adventures with XOOPSOOP Adventures with XOOPS
OOP Adventures with XOOPS
 
springdatajpa-up.pdf
springdatajpa-up.pdfspringdatajpa-up.pdf
springdatajpa-up.pdf
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Sql server basics
Sql server basicsSql server basics
Sql server basics
 
Building node.js applications with Database Jones
Building node.js applications with Database JonesBuilding node.js applications with Database Jones
Building node.js applications with Database Jones
 
Software development - the java perspective
Software development - the java perspectiveSoftware development - the java perspective
Software development - the java perspective
 
Php summary
Php summaryPhp summary
Php summary
 
CodeIgniter & MVC
CodeIgniter & MVCCodeIgniter & MVC
CodeIgniter & MVC
 
Hibernate jj
Hibernate jjHibernate jj
Hibernate jj
 
Windows Azure and a little SQL Data Services
Windows Azure and a little SQL Data ServicesWindows Azure and a little SQL Data Services
Windows Azure and a little SQL Data Services
 
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File ServerUKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
UKOUG 2011 - Drag, Drop and other Stuff. Using your Database as a File Server
 
Sqllite
SqlliteSqllite
Sqllite
 
Java Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : DatastoreJava Web Programming on Google Cloud Platform [2/3] : Datastore
Java Web Programming on Google Cloud Platform [2/3] : Datastore
 

Mais de Ivano Malavolta

Mais de Ivano Malavolta (20)

Conducting Experiments on the Software Architecture of Robotic Systems (QRARS...
Conducting Experiments on the Software Architecture of Robotic Systems (QRARS...Conducting Experiments on the Software Architecture of Robotic Systems (QRARS...
Conducting Experiments on the Software Architecture of Robotic Systems (QRARS...
 
The H2020 experience
The H2020 experienceThe H2020 experience
The H2020 experience
 
The Green Lab - Research cocktail @Vrije Universiteit Amsterdam (October 2020)
The Green Lab - Research cocktail  @Vrije Universiteit Amsterdam (October 2020)The Green Lab - Research cocktail  @Vrije Universiteit Amsterdam (October 2020)
The Green Lab - Research cocktail @Vrije Universiteit Amsterdam (October 2020)
 
Software sustainability and Green IT
Software sustainability and Green ITSoftware sustainability and Green IT
Software sustainability and Green IT
 
Navigation-aware and Personalized Prefetching of Network Requests in Android ...
Navigation-aware and Personalized Prefetching of Network Requests in Android ...Navigation-aware and Personalized Prefetching of Network Requests in Android ...
Navigation-aware and Personalized Prefetching of Network Requests in Android ...
 
How Maintainability Issues of Android Apps Evolve [ICSME 2018]
How Maintainability Issues of Android Apps Evolve [ICSME 2018]How Maintainability Issues of Android Apps Evolve [ICSME 2018]
How Maintainability Issues of Android Apps Evolve [ICSME 2018]
 
Collaborative Model-Driven Software Engineering: a Classification Framework a...
Collaborative Model-Driven Software Engineering: a Classification Framework a...Collaborative Model-Driven Software Engineering: a Classification Framework a...
Collaborative Model-Driven Software Engineering: a Classification Framework a...
 
Experimenting on Mobile Apps Quality - a tale about Energy, Performance, and ...
Experimenting on Mobile Apps Quality - a tale about Energy, Performance, and ...Experimenting on Mobile Apps Quality - a tale about Energy, Performance, and ...
Experimenting on Mobile Apps Quality - a tale about Energy, Performance, and ...
 
Modeling objects interaction via UML sequence diagrams [Software Design] [Com...
Modeling objects interaction via UML sequence diagrams [Software Design] [Com...Modeling objects interaction via UML sequence diagrams [Software Design] [Com...
Modeling objects interaction via UML sequence diagrams [Software Design] [Com...
 
Modeling behaviour via UML state machines [Software Design] [Computer Science...
Modeling behaviour via UML state machines [Software Design] [Computer Science...Modeling behaviour via UML state machines [Software Design] [Computer Science...
Modeling behaviour via UML state machines [Software Design] [Computer Science...
 
Object-oriented design patterns in UML [Software Design] [Computer Science] [...
Object-oriented design patterns in UML [Software Design] [Computer Science] [...Object-oriented design patterns in UML [Software Design] [Computer Science] [...
Object-oriented design patterns in UML [Software Design] [Computer Science] [...
 
Structure modeling with UML [Software Design] [Computer Science] [Vrije Unive...
Structure modeling with UML [Software Design] [Computer Science] [Vrije Unive...Structure modeling with UML [Software Design] [Computer Science] [Vrije Unive...
Structure modeling with UML [Software Design] [Computer Science] [Vrije Unive...
 
Requirements engineering with UML [Software Design] [Computer Science] [Vrije...
Requirements engineering with UML [Software Design] [Computer Science] [Vrije...Requirements engineering with UML [Software Design] [Computer Science] [Vrije...
Requirements engineering with UML [Software Design] [Computer Science] [Vrije...
 
Modeling and abstraction, software development process [Software Design] [Com...
Modeling and abstraction, software development process [Software Design] [Com...Modeling and abstraction, software development process [Software Design] [Com...
Modeling and abstraction, software development process [Software Design] [Com...
 
[2017/2018] Agile development
[2017/2018] Agile development[2017/2018] Agile development
[2017/2018] Agile development
 
Reconstructing microservice-based architectures
Reconstructing microservice-based architecturesReconstructing microservice-based architectures
Reconstructing microservice-based architectures
 
[2017/2018] AADL - Architecture Analysis and Design Language
[2017/2018] AADL - Architecture Analysis and Design Language[2017/2018] AADL - Architecture Analysis and Design Language
[2017/2018] AADL - Architecture Analysis and Design Language
 
[2017/2018] Architectural languages
[2017/2018] Architectural languages[2017/2018] Architectural languages
[2017/2018] Architectural languages
 
[2017/2018] Introduction to Software Architecture
[2017/2018] Introduction to Software Architecture[2017/2018] Introduction to Software Architecture
[2017/2018] Introduction to Software Architecture
 
[2017/2018] RESEARCH in software engineering
[2017/2018] RESEARCH in software engineering[2017/2018] RESEARCH in software engineering
[2017/2018] RESEARCH in software engineering
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Local Storage

  • 1. Local Storage Ivano Malavolta ivano.malavolta@univaq.it http://www.di.univaq.it/malavolta
  • 2. Roadmap • Introduction • Web Storage • WebSQL • IndexedDB • File System Access • Final Considerations
  • 3. Introduction There are 4 ways to store data locally • Web storage – Local Storage – Session Storage • WebSQL • Indexed DB • File System Access
  • 4. Web Storage LocalStorage stores data in key/value pairs persists across browser sessions SessionStorage stores data in key/value pairs data is erased when a browser session ends
  • 5. Introduction WebSQL Database relational DB support for tables creation, insert, update, … transactional persists across browser sessions Its evolution is called IndexedDB but it is actually IndexedDB, not supported by most mobile browsers
  • 6. Introduction File System Access you can access files locally to your app supports main FS operations – files creation, move, delete, rename, creation, etc. it is not transactional persists across browser sessions
  • 8. Roadmap • Introduction • Web Storage • WebSQL • IndexedDB • File System Access • Final Considerations
  • 9. Web Storage It is based on a single persistent object called localStorage You can set values by calling window.localStorage.setItem(“name”, “Ivano”); You can get values back by calling var name = window.localStorage.getItem(“name”);
  • 10. Supported Methods localStorage.key(0) Returns the name of the key at the position specified getItem(“key”) Returns the item identified by it's key setItem(“key”, “value”) Saves and item at the key provided removeItem(“hey”) Removes the item identified by it's key clear() Removes all of the key value pairs
  • 11. Complex Objects Current implementations support only string-to-string mappings you can store only strings keys can be only strings You can use JSON serialization if you need to store complex data structures
  • 12. Example of JSON Serialization // simple class declaration function Person(name, surname) { this.name = name; this.surname = surname; } // object creation var user = new Person(‘Ivano’, ‘Malavolta’); // object serialization window.localStorage.setItem(“user”, JSON.stringify(user)); // object retrieval var current = JSON.parse(window.localStorage.getItem(“user”));
  • 13. Checking Existence You can simply check if the needed element is == null if (window.localStorage.getItem(“user”)) { // there is an object in user } else { // the user key does not have any value }
  • 14. Selecting elements In this case you have to manually iterate on elements var users = [...]; // array of Person objects window.localStorage.setItem(“users”, JSON.stringify(users)); var allUsers = JSON.parse(window.localStorage.getItem(“users”)); var ivanos = []; for(var i=0; i<allUsers.length; i++) { if(allUsers[i].name == ‘Ivano’) ivanos.push(allUsers[i]); }
  • 15. Counting Elements Also in this case, we have to do it manually var usersNumber = JSON.parse(window.localStorage.getItem(“users“)).length;
  • 16. Session Storage Session Storage provides the same interface as Local Storage you can call the same methods but Session Storage is cleared between app launches
  • 17. Roadmap • Introduction • Web Storage • WebSQL • IndexedDB • File System Access • Final Considerations
  • 18. WebSQL It provides you a structured SQL relational database You have to setup a DB schema You can then perform classical SQL queries tx.executeSql("SELECT * FROM User“, [], function(tx, result) { // callback code });
  • 19. Opening a DB Done via a dedicated function var db = openDatabase(‘Test', ‘1.0', ‘Test DB', 100000); It creates a new SQLLite DB and returns a new Database object The Database object will be used to manipulate the data
  • 20. Opening a DB: syntax openDatabase(name, version, displayname, size); DB name the name of the DB DB version the version of the DB DB Display name the display name of the DB DB Size the size of the DB in bytes
  • 21. Database It allows to manipulate the data via 2 methods: changeVersion atomically verify the version number and change it db.changeVersion("1.0", "1.1"); transaction performs a DB transaction
  • 22. Transactions It allow you to execute SQL statements against the DB db.transaction(queries, error, success); 3 callback functions: queries : contains the queries to be performed error : executed if the transaction results in an error success : executed if the transaction terminates correctly
  • 24. executeSql It is the method that performs a SQL statement The user can build up a database transaction by calling the executeSql method multiple times function populateDB(tx) { tx.executeSql('DROP TABLE IF EXISTS USER'); tx.executeSql('CREATE TABLE IF NOT EXISTS USER (id unique, name, surname)'); tx.executeSql('INSERT INTO USER(id, name, surname) VALUES (1, ?, ?)‘, [“Ivano“, “Malavolta“], success, error); }
  • 25. Result Sets When the executeSql method is called, it will invoke it's callback with a SQLResultSet parameter It has 3 properties: insertId the ID of the row that has been inserted rowsAffected the number of rows changed by the SQL statement rows the data returned from a SQL select statement rows is an object of type SQLResultSetList
  • 26. Results Sets Example ... tx.executeSql('INSERT INTO USER(id, name,surname) VALUES (1, ?, ?)‘, [“Ivano“, “Malavolta“], success, error); } function success(tx, results) { var affected = results.rowsAffected(); // 1 } function error(err) { // code for managing the error }
  • 27. Result Set Lists It contains the data returned from a SQL Select statement length the number of rows returned by the SQL query item(index) returns the row at the specified index represented by a JavaScript object
  • 28. Result Set List Example ... tx.executeSql(‘SELECT * FROM USER‘, [], success, error); } function success(tx, results) { var size = results.rows.length; for (var i=0; i<size; i++){ console.log(results.rows.item(i).name); } }
  • 29. Errors It contains information about an occurred error code A predefined error code es. UNKNOWN_ERR, DATABASE_ERR, TOO_LARGE_ERR, QUOTA_ERR, SYNTAX_ERR, TIMEOUT_ERR message A description of the error
  • 30. Error Code Example ... tx.executeSql(‘SELECT * FROM USER‘,[], success, error); } function error(err) { console.log(err.code); }
  • 31. Roadmap • Introduction • Web Storage • WebSQL • IndexedDB • File System Access • Final Considerations
  • 32. Indexed DB It tries to combine Web Storage and WebSQL You can save data as key/value pairs key/value You can define multiple DBs Good Performance data is indexed asynchronous it does not block the UI
  • 33. Indexed DB An Indexed DB is a collection of object stores You can drop objects into the stores You can see stores as a big SQL table with only key/value key/value pairs you don’t need to define a schema upfront
  • 34. Roadmap • Introduction • Web Storage • WebSQL • IndexedDB • File System Access • Final Considerations
  • 35. File System Access It allows you to read, write and navigate file system hierarchies It is fundamental for managing and storing large files client- and binary content on the client-side
  • 36. File System Access Workflow 1. Request file system access – persistent or temporary file system 2. then you can perform CRUD operations for both files and folders: – Create – Read – Update – Delete
  • 37. Request File System requestFileSystem(type, size, successCb, [errorCb]) type LocalFileSystem.TEMPORARY LocalFileSystem .PERSISTENT size size in bytes the app will require for storage. successCb success callback, its argument is a FileSystem object ErrorCb error callback, its argument is a FileError object
  • 38. Temporary VS Persistent Temporary the files stored by the app can be deleted at the browser’s discretion no guarantee of persistence Persistent cannot be deleted by the browser without authorization by the app
  • 39. Local File System Example window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, onSuccess, onError); function onSuccess(fileSystem) { console.log(fileSystem.name); }
  • 40. File System The FileSystem object has 2 properties: name the name of the file system it is unique across the list of exposed file systems root the DirectoryEntry object representing the root folder of the file system
  • 41. Resolving a File URI resolveLocalFileSystemURI retrieve a DirectoryEntry or FileEntry using a URI window.resolveLocalFileSystemURI( "file:///userImg.png", onSuccess, onError); function onSuccess(fileEntry) { console.log(fileEntry.name); }
  • 42. Entities FileEntry The real objects DirectoryEntry File Descriptor FileReader FileWriter Writing & Reading objects DirectoryReader
  • 43. File Entry It represents a file on a file system isFile (boolean) Always true isDirectory (boolean) Always false name (DOMString) the name of the FileEntry, excluding the path fullPath (DOMString) the full absolute path from the root
  • 44. File Entry Methods getMetadata(success, getMetadata(success, fail) Look up metadata about a file moveTo(parentEntry, newName, success, fail) moveTo( parentEntry, newName, Move a file to a different location on the file system copyTo(parentEntry, newName, success, fail) copyTo(parentEntry, newName, Copy a file to a different location on the file system toURI() toURI() Return a URI that can be used to locate a file
  • 45. File Entry Methods remove(success, fail) Delete a file getParent(success, getParent(success, fail) Look up the parent directory createWriter(success, createWriter(success, fail) Creates a FileWriter object that can be used to write to a file file(success, fail) Creates a File object containing file properties
  • 46. File It contains attributes of a single file name (DOMString) The name of the file fullPath (DOMString) The full path of the file including the file name type (DOMString) The mime type of the file lastModifiedDate (Date) The last time the file was modified size (long) The size of the file in bytes
  • 47. Directory Entry It represents a directory on a file system It has the same properties of FileEntry
  • 48. Directory Entry Methods getMetadata(success, getMetadata(success, fail) Look up metadata about a directory moveTo(parentEntry, newName, moveTo(parentEntry , newName, success, fail) Move a directory to a different location on the file system copyTo(parentEntry, newName, copyTo(parentEntry, newName, success, fail) Copy a directory to a different location on the file system toURI() toURI() Return a URI that can be used to locate a directory
  • 49. Directory Entry Methods getParent(success, fail) getParent(success, Look up the parent directory createReader() createReader() Creates a DirectoryReader object that can be used to read a directory getDirectory(path, options, success, fail) getDirectory(path, Create or look up a directory options: create (true | false) exclusive (true | false)
  • 50. Directory Entry Methods getFile(path, options, success, fail) getFile(path, Create or look up a file within the directory options: create (true | false) exclusive (true | false) removeRecursively(success, fail) removeRecursively(success, Delete a directory and all of its contents
  • 51. File Reader It is used to read the contents of a file Files can be read as text or as a base64 data encoded string You can also abort() e file reading activity You can register your own event listeners to receive the following events: loadstart, progress, load, loadend, error, abort
  • 52. File Reader Example entry.file(win, fail); function win(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log(evt.target.result); }; reader.readAsText(file); // reader.abort(); }; function fail(evt) { console.log(error.code); };
  • 53. File Writer It is used to write to a file The data to be written must be UTF-8 encoded UTF- You can register your own event listeners to receive the following events: writestart, progress, write, writeend, error, abort
  • 54. File Writer A FileWriter is created for a single file You can use it to write to a file multiple times the FileWriter maintains the file's position and length attributes, so you can seek and write anywhere in the file By default, the FileWriter writes to the beginning of the file (will overwrite existing data) Set the optional append boolean to true in the FileWriter's constructor to begin writing to the end of the file
  • 55. File Writer Methods abort() Aborts writing file seek(byte) Moves the file pointer to the byte specified. truncate(length) Shortens the file to the length specified. write(data) Writes data to the file
  • 56. File Writer Example entry.createWriter(win, fail); function win(writer) { writer.onwrite = function(evt) { console.log(“ok"); }; writer.write(“Ivano Malavolta"); }; function fail(evt) { // error management };
  • 57. Directory Reader It is an object that lists files and directories in a directory it has only one method: readEntries(success, readEntries(success, fail) Read the entries of the directory
  • 58. Directory Reader Example var directoryReader = dirEntry.createReader(); directoryReader.readEntries(success, fail); function success(entries) { var i; for (i=0; i<entries.length; i++) { console.log(entries[i].name); } } function fail(error) { console.log(error.code); }
  • 59. A Final Example: Looking for a file and reading it window.requestFileSystem(window.PERSISTENT, 0, initFS, error); function initFS(fs) { fs.root.getFile(‘log.txt', {}, win, error); } function win(fileEntry) { fileEntry.file(read, error); } function read(file) { var reader = new FileReader(); reader.onloadend = function(e) { console.log(this.result); }; reader.readAsText(file); } function error(err) { console.log(err);}
  • 60. Roadmap • Introduction • Web Storage • WebSQL • IndexedDB • File System Access • Final Considerations
  • 61. Final Considerations You will likely use all of them in combination Use the right API for the right job Web Storage • it is not transactional race conditions • very simple API, no schema • only String data performance issues for complex data due to JSON serialization • session storage will be cleared after the app is closed • 5Mb quota
  • 62. Final Considerations WebSQL • SQL-based fast and efficient • transactional more robust • asynchronous does not block the UI • rigid data structure data integrity vs agility • 5Mb quota
  • 63. Final Considerations IndexedDB • simple data model easy to use • transactional more robust • asynchronous does not block the UI • good search performance indexed data • data is unstructured integrity problems • 5Mb quota
  • 64. Final Considerations File System • asynchronous does not block the UI • not transactional • indexing and search are not built-in you have to implement your lookup functions • unlimited storage – useful for images, videos, documents, etc.