SlideShare a Scribd company logo
Storage, FileSystem & SQL
Storage, FileSystem & SQL
✦There are several ways to store data in
Codename One, in this lesson I’ll break them
down
✦Each option has tradeoffs that should be weighed
carefully
Storage
✦If you are unsure about what you want to use Storage
is probably the best place to start
✦It’s simplest. Like a flat file system
✦When an app is uninstalled it’s deleted (notice it might
be restored from backup on reinstall)
✦In some OS’s (and simulator) it is implemented on top
of the regular file system but that is an implementation
detail! Don’t rely on that!
✦Storage can be seamlessly encrypted
FileSystemStorage
✦A map of the native OS filesystem
✦Hierarchy based, always uses / as separator and
always requires full paths
✦Can contain special roots e.g. SD card, caches
✦Behavior is often OS specific
✦Because of the low level nature features like seamless
encryption aren’t available (although it’s possible to
encrypt anything)
Storage FileSystemStorage
Portable Access to native features
Flat Hierarchy, always use full paths
Cleaned on uninstall Behavior varies on uninstall
Private to application Potentially exposed in some locations
Cached access Direct access
Great for small (cachable) files Great for larger files
Which Should I Pick?
serverInstance.getItemsAsync(items -> {
shoppingList.removeAll();
shoppingList.add(filler);
ArrayList<Map<String, Object>> data = new ArrayList<>();
for(Item i : items) {
if(!i.isDeleted()) {
Component c = createCheck(i.getName(), i.isMarked());
shoppingList.add(c);
shoppingList.add(createSeparator());
data.add(i.toMap());
}
}
shoppingList.revalidate();
// cache the data
try(OutputStream os = Storage.getInstance().createOutputStream("CachedData")) {
Map<String, Object> m = new HashMap<>();
m.put("root", data);
os.write(Result.fromContent(m).toString().getBytes("UTF-8"));
} catch(IOException err) {
Log.e(err);
}
});
Caching Results from the Server
serverInstance.getItemsAsync(items -> {
shoppingList.removeAll();
shoppingList.add(filler);
ArrayList<Map<String, Object>> data = new ArrayList<>();
for(Item i : items) {
if(!i.isDeleted()) {
Component c = createCheck(i.getName(), i.isMarked());
shoppingList.add(c);
shoppingList.add(createSeparator());
data.add(i.toMap());
}
}
shoppingList.revalidate();
// cache the data
try(OutputStream os = Storage.getInstance().createOutputStream("CachedData")) {
Map<String, Object> m = new HashMap<>();
m.put("root", data);
os.write(Result.fromContent(m).toString().getBytes("UTF-8"));
} catch(IOException err) {
Log.e(err);
}
});
Caching Results from the Server
Progress Change
I replaced the removal of the
progress indicator as the list
can now include many
arbitrary entries
serverInstance.getItemsAsync(items -> {
shoppingList.removeAll();
shoppingList.add(filler);
ArrayList<Map<String, Object>> data = new ArrayList<>();
for(Item i : items) {
if(!i.isDeleted()) {
Component c = createCheck(i.getName(), i.isMarked());
shoppingList.add(c);
shoppingList.add(createSeparator());
data.add(i.toMap());
}
}
shoppingList.revalidate();
// cache the data
try(OutputStream os = Storage.getInstance().createOutputStream("CachedData")) {
Map<String, Object> m = new HashMap<>();
m.put("root", data);
os.write(Result.fromContent(m).toString().getBytes("UTF-8"));
} catch(IOException err) {
Log.e(err);
}
});
Caching Results from the Server
serverInstance.getItemsAsync(items -> {
shoppingList.removeAll();
shoppingList.add(filler);
ArrayList<Map<String, Object>> data = new ArrayList<>();
for(Item i : items) {
if(!i.isDeleted()) {
Component c = createCheck(i.getName(), i.isMarked());
shoppingList.add(c);
shoppingList.add(createSeparator());
data.add(i.toMap());
}
}
shoppingList.revalidate();
// cache the data
try(OutputStream os = Storage.getInstance().createOutputStream("CachedData")) {
Map<String, Object> m = new HashMap<>();
m.put("root", data);
os.write(Result.fromContent(m).toString().getBytes("UTF-8"));
} catch(IOException err) {
Log.e(err);
}
});
Caching Results from the Server
Storage
I’m storing the cached data
as JSON because I already
have the code that does that
transport
if(Storage.getInstance().exists("CachedData")) {
JSONParser.setUseLongs(true);
JSONParser p = new JSONParser();
try(Reader r = new InputStreamReader(Storage.getInstance().createInputStream("CachedData"), "UTF-8")) {
Map<String, Object> result = p.parseJSON(r);
List<Map<String, Object>> lst = (List<Map<String, Object>>)result.get("root");
for(Map<String, Object> itm : lst) {
Item i = new Item(itm);
if(!i.isDeleted()) {
Component c = createCheck(i.getName(), i.isMarked());
shoppingList.add(c);
shoppingList.add(createSeparator());
}
}
shoppingList.revalidate();
} catch(IOException err) {
Log.e(err);
}
} else {
Component ip = FlowLayout.encloseCenterMiddle(new InfiniteProgress());
shoppingList.add(ip);
}
Loading Cached Server Results
SQLite
© Codename One 2017 all rights reserved
SQLite
✦SQLite is a native C embeddable database engine
that’s available almost everywhere
✦It’s very customizable and as a result implementations
on various OS’s aren’t completely consistent
✦It’s available in Android, iOS, UWP & JavaScript
✦SQLite is useful if you need a “real” database on the
device where you can perform fast queries
SQLite Device Fragmentation
✦The Android version is threadsafe where the iOS
version is not e.g. even the GC can crash SQLite on
iOS if it closes a cursor from the GC thread
✦The Android & iOS versions handle transaction
isolation differently
✦In JavaScript you can’t ship an app with your own
database
Shipping an App With a Database
✦One of the common use cases for SQLite is to ship
an app with a ready made database (e.g. nutritional
database) within the JAR:
String path = Display.getInstance().getDatabasePath("MyDB.db");
if(path != null && !FileSystemStorage.getInstance().exists(path)) {
try(InputStream i = Display.getInstance().getResourceAsStream(getClass(), "/MyDB.db");
OutputStream o = FileSystemStorage.getInstance().openOutputStream(path)) {
Util.copy(i, o);
} catch(IOException err) {
Log.e(err);
}
}
Database db = null;
Cursor cur = null;
try {
db = Display.getInstance().openOrCreate("MyDB.db");
cur = db.executeQuery(sqlStatement);
while(cur.next()) {
Row r = cur.getRow();
String s = r.getString(cur.getColumnIndex("columnName"));
// ....
}
} catch(IOException err) {
Log.e(err);
ToastBar.showErrorMessage("Error: " + err);
} finally {
Util.cleanup(db);
Util.cleanup(cur);
}
Issue a Query
Database db = null;
Cursor cur = null;
try {
db = Display.getInstance().openOrCreate("MyDB.db");
cur = db.executeQuery(sqlStatement);
while(cur.next()) {
Row r = cur.getRow();
String s = r.getString(cur.getColumnIndex("columnName"));
// ....
}
} catch(IOException err) {
Log.e(err);
ToastBar.showErrorMessage("Error: " + err);
} finally {
Util.cleanup(db);
Util.cleanup(cur);
}
Issue a Query
Cleanup
It’s crucial to cleanup
properly. Otherwise on iOS
the app might crash as the
GC might cleanup before your
code causing a thread conflict
What did we learn?
✦When to choose file system storage and when to
choose storage
✦How to implement data caching/offline mode
using storage
✦The problems and value proposition of SQLite
Thank You

More Related Content

Similar to Storage, FileSystem & SQL.pdf

Matthew Vignau: Memory Management in SharePoint 2007 Development
Matthew Vignau: Memory Management in SharePoint 2007 DevelopmentMatthew Vignau: Memory Management in SharePoint 2007 Development
Matthew Vignau: Memory Management in SharePoint 2007 Development
SharePoint Saturday NY
 
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardAndroid | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
JAX London
 
OWB11gR2 - Extending ETL
OWB11gR2 - Extending ETL OWB11gR2 - Extending ETL
OWB11gR2 - Extending ETL
Suraj Bang
 
Compass Framework
Compass FrameworkCompass Framework
Compass Framework
Lukas Vlcek
 
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NCAndroid Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Jim Tochterman
 
How do I - Storage, FileSystem & SQL - Transcript.pdf
How do I - Storage, FileSystem & SQL - Transcript.pdfHow do I - Storage, FileSystem & SQL - Transcript.pdf
How do I - Storage, FileSystem & SQL - Transcript.pdf
ShaiAlmog1
 
第一次用Parse就深入淺出
第一次用Parse就深入淺出第一次用Parse就深入淺出
第一次用Parse就深入淺出
Ymow Wu
 
Itemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integrationItemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integration
{item:foo}
 
Assetic (Zendcon)
Assetic (Zendcon)Assetic (Zendcon)
Assetic (Zendcon)
Kris Wallsmith
 
Core Data with Swift 3.0
Core Data with Swift 3.0Core Data with Swift 3.0
Core Data with Swift 3.0
Korhan Bircan
 
NoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices -  Michael HacksteinNoSQL meets Microservices -  Michael Hackstein
NoSQL meets Microservices - Michael Hackstein
distributed matters
 
Quick and Easy Development with Node.js and Couchbase Server
Quick and Easy Development with Node.js and Couchbase ServerQuick and Easy Development with Node.js and Couchbase Server
Quick and Easy Development with Node.js and Couchbase Server
Nic Raboy
 
Express Presentation
Express PresentationExpress Presentation
Express Presentation
aaronheckmann
 
Offline strategies for HTML5 web applications - pfCongres2012
Offline strategies for HTML5 web applications - pfCongres2012Offline strategies for HTML5 web applications - pfCongres2012
Offline strategies for HTML5 web applications - pfCongres2012
Stephan Hochdörfer
 
Bkbiet day2 & 3
Bkbiet day2 & 3Bkbiet day2 & 3
Bkbiet day2 & 3
mihirio
 
Android Data Storagefinal
Android Data StoragefinalAndroid Data Storagefinal
Android Data Storagefinal
Nakka Srilakshmi
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
Igor Bronovskyy
 
Bubbles – Virtual Data Objects
Bubbles – Virtual Data ObjectsBubbles – Virtual Data Objects
Bubbles – Virtual Data Objects
Stefan Urbanek
 
Sharepoint Saturday India Online best practice for developing share point sol...
Sharepoint Saturday India Online best practice for developing share point sol...Sharepoint Saturday India Online best practice for developing share point sol...
Sharepoint Saturday India Online best practice for developing share point sol...
Shakir Majeed Khan
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介
Jace Ju
 

Similar to Storage, FileSystem & SQL.pdf (20)

Matthew Vignau: Memory Management in SharePoint 2007 Development
Matthew Vignau: Memory Management in SharePoint 2007 DevelopmentMatthew Vignau: Memory Management in SharePoint 2007 Development
Matthew Vignau: Memory Management in SharePoint 2007 Development
 
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted NewardAndroid | Busy Java Developers Guide to Android: Persistence | Ted Neward
Android | Busy Java Developers Guide to Android: Persistence | Ted Neward
 
OWB11gR2 - Extending ETL
OWB11gR2 - Extending ETL OWB11gR2 - Extending ETL
OWB11gR2 - Extending ETL
 
Compass Framework
Compass FrameworkCompass Framework
Compass Framework
 
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NCAndroid Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
Android Development w/ ArcGIS Server - Esri Dev Meetup - Charlotte, NC
 
How do I - Storage, FileSystem & SQL - Transcript.pdf
How do I - Storage, FileSystem & SQL - Transcript.pdfHow do I - Storage, FileSystem & SQL - Transcript.pdf
How do I - Storage, FileSystem & SQL - Transcript.pdf
 
第一次用Parse就深入淺出
第一次用Parse就深入淺出第一次用Parse就深入淺出
第一次用Parse就深入淺出
 
Itemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integrationItemscript, a specification for RESTful JSON integration
Itemscript, a specification for RESTful JSON integration
 
Assetic (Zendcon)
Assetic (Zendcon)Assetic (Zendcon)
Assetic (Zendcon)
 
Core Data with Swift 3.0
Core Data with Swift 3.0Core Data with Swift 3.0
Core Data with Swift 3.0
 
NoSQL meets Microservices - Michael Hackstein
NoSQL meets Microservices -  Michael HacksteinNoSQL meets Microservices -  Michael Hackstein
NoSQL meets Microservices - Michael Hackstein
 
Quick and Easy Development with Node.js and Couchbase Server
Quick and Easy Development with Node.js and Couchbase ServerQuick and Easy Development with Node.js and Couchbase Server
Quick and Easy Development with Node.js and Couchbase Server
 
Express Presentation
Express PresentationExpress Presentation
Express Presentation
 
Offline strategies for HTML5 web applications - pfCongres2012
Offline strategies for HTML5 web applications - pfCongres2012Offline strategies for HTML5 web applications - pfCongres2012
Offline strategies for HTML5 web applications - pfCongres2012
 
Bkbiet day2 & 3
Bkbiet day2 & 3Bkbiet day2 & 3
Bkbiet day2 & 3
 
Android Data Storagefinal
Android Data StoragefinalAndroid Data Storagefinal
Android Data Storagefinal
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
 
Bubbles – Virtual Data Objects
Bubbles – Virtual Data ObjectsBubbles – Virtual Data Objects
Bubbles – Virtual Data Objects
 
Sharepoint Saturday India Online best practice for developing share point sol...
Sharepoint Saturday India Online best practice for developing share point sol...Sharepoint Saturday India Online best practice for developing share point sol...
Sharepoint Saturday India Online best practice for developing share point sol...
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介
 

More from ShaiAlmog1

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
ShaiAlmog1
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
ShaiAlmog1
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
ShaiAlmog1
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
ShaiAlmog1
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
ShaiAlmog1
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
ShaiAlmog1
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
ShaiAlmog1
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
ShaiAlmog1
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
ShaiAlmog1
 

More from ShaiAlmog1 (20)

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdf
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
 

Recently uploaded

Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Public CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptxPublic CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptx
marufrahmanstratejm
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 

Recently uploaded (20)

Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Public CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptxPublic CyberSecurity Awareness Presentation 2024.pptx
Public CyberSecurity Awareness Presentation 2024.pptx
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 

Storage, FileSystem & SQL.pdf

  • 2. Storage, FileSystem & SQL ✦There are several ways to store data in Codename One, in this lesson I’ll break them down ✦Each option has tradeoffs that should be weighed carefully
  • 3. Storage ✦If you are unsure about what you want to use Storage is probably the best place to start ✦It’s simplest. Like a flat file system ✦When an app is uninstalled it’s deleted (notice it might be restored from backup on reinstall) ✦In some OS’s (and simulator) it is implemented on top of the regular file system but that is an implementation detail! Don’t rely on that! ✦Storage can be seamlessly encrypted
  • 4. FileSystemStorage ✦A map of the native OS filesystem ✦Hierarchy based, always uses / as separator and always requires full paths ✦Can contain special roots e.g. SD card, caches ✦Behavior is often OS specific ✦Because of the low level nature features like seamless encryption aren’t available (although it’s possible to encrypt anything)
  • 5. Storage FileSystemStorage Portable Access to native features Flat Hierarchy, always use full paths Cleaned on uninstall Behavior varies on uninstall Private to application Potentially exposed in some locations Cached access Direct access Great for small (cachable) files Great for larger files Which Should I Pick?
  • 6. serverInstance.getItemsAsync(items -> { shoppingList.removeAll(); shoppingList.add(filler); ArrayList<Map<String, Object>> data = new ArrayList<>(); for(Item i : items) { if(!i.isDeleted()) { Component c = createCheck(i.getName(), i.isMarked()); shoppingList.add(c); shoppingList.add(createSeparator()); data.add(i.toMap()); } } shoppingList.revalidate(); // cache the data try(OutputStream os = Storage.getInstance().createOutputStream("CachedData")) { Map<String, Object> m = new HashMap<>(); m.put("root", data); os.write(Result.fromContent(m).toString().getBytes("UTF-8")); } catch(IOException err) { Log.e(err); } }); Caching Results from the Server
  • 7. serverInstance.getItemsAsync(items -> { shoppingList.removeAll(); shoppingList.add(filler); ArrayList<Map<String, Object>> data = new ArrayList<>(); for(Item i : items) { if(!i.isDeleted()) { Component c = createCheck(i.getName(), i.isMarked()); shoppingList.add(c); shoppingList.add(createSeparator()); data.add(i.toMap()); } } shoppingList.revalidate(); // cache the data try(OutputStream os = Storage.getInstance().createOutputStream("CachedData")) { Map<String, Object> m = new HashMap<>(); m.put("root", data); os.write(Result.fromContent(m).toString().getBytes("UTF-8")); } catch(IOException err) { Log.e(err); } }); Caching Results from the Server Progress Change I replaced the removal of the progress indicator as the list can now include many arbitrary entries
  • 8. serverInstance.getItemsAsync(items -> { shoppingList.removeAll(); shoppingList.add(filler); ArrayList<Map<String, Object>> data = new ArrayList<>(); for(Item i : items) { if(!i.isDeleted()) { Component c = createCheck(i.getName(), i.isMarked()); shoppingList.add(c); shoppingList.add(createSeparator()); data.add(i.toMap()); } } shoppingList.revalidate(); // cache the data try(OutputStream os = Storage.getInstance().createOutputStream("CachedData")) { Map<String, Object> m = new HashMap<>(); m.put("root", data); os.write(Result.fromContent(m).toString().getBytes("UTF-8")); } catch(IOException err) { Log.e(err); } }); Caching Results from the Server
  • 9. serverInstance.getItemsAsync(items -> { shoppingList.removeAll(); shoppingList.add(filler); ArrayList<Map<String, Object>> data = new ArrayList<>(); for(Item i : items) { if(!i.isDeleted()) { Component c = createCheck(i.getName(), i.isMarked()); shoppingList.add(c); shoppingList.add(createSeparator()); data.add(i.toMap()); } } shoppingList.revalidate(); // cache the data try(OutputStream os = Storage.getInstance().createOutputStream("CachedData")) { Map<String, Object> m = new HashMap<>(); m.put("root", data); os.write(Result.fromContent(m).toString().getBytes("UTF-8")); } catch(IOException err) { Log.e(err); } }); Caching Results from the Server Storage I’m storing the cached data as JSON because I already have the code that does that transport
  • 10. if(Storage.getInstance().exists("CachedData")) { JSONParser.setUseLongs(true); JSONParser p = new JSONParser(); try(Reader r = new InputStreamReader(Storage.getInstance().createInputStream("CachedData"), "UTF-8")) { Map<String, Object> result = p.parseJSON(r); List<Map<String, Object>> lst = (List<Map<String, Object>>)result.get("root"); for(Map<String, Object> itm : lst) { Item i = new Item(itm); if(!i.isDeleted()) { Component c = createCheck(i.getName(), i.isMarked()); shoppingList.add(c); shoppingList.add(createSeparator()); } } shoppingList.revalidate(); } catch(IOException err) { Log.e(err); } } else { Component ip = FlowLayout.encloseCenterMiddle(new InfiniteProgress()); shoppingList.add(ip); } Loading Cached Server Results
  • 11. SQLite © Codename One 2017 all rights reserved
  • 12. SQLite ✦SQLite is a native C embeddable database engine that’s available almost everywhere ✦It’s very customizable and as a result implementations on various OS’s aren’t completely consistent ✦It’s available in Android, iOS, UWP & JavaScript ✦SQLite is useful if you need a “real” database on the device where you can perform fast queries
  • 13. SQLite Device Fragmentation ✦The Android version is threadsafe where the iOS version is not e.g. even the GC can crash SQLite on iOS if it closes a cursor from the GC thread ✦The Android & iOS versions handle transaction isolation differently ✦In JavaScript you can’t ship an app with your own database
  • 14. Shipping an App With a Database ✦One of the common use cases for SQLite is to ship an app with a ready made database (e.g. nutritional database) within the JAR: String path = Display.getInstance().getDatabasePath("MyDB.db"); if(path != null && !FileSystemStorage.getInstance().exists(path)) { try(InputStream i = Display.getInstance().getResourceAsStream(getClass(), "/MyDB.db"); OutputStream o = FileSystemStorage.getInstance().openOutputStream(path)) { Util.copy(i, o); } catch(IOException err) { Log.e(err); } }
  • 15. Database db = null; Cursor cur = null; try { db = Display.getInstance().openOrCreate("MyDB.db"); cur = db.executeQuery(sqlStatement); while(cur.next()) { Row r = cur.getRow(); String s = r.getString(cur.getColumnIndex("columnName")); // .... } } catch(IOException err) { Log.e(err); ToastBar.showErrorMessage("Error: " + err); } finally { Util.cleanup(db); Util.cleanup(cur); } Issue a Query
  • 16. Database db = null; Cursor cur = null; try { db = Display.getInstance().openOrCreate("MyDB.db"); cur = db.executeQuery(sqlStatement); while(cur.next()) { Row r = cur.getRow(); String s = r.getString(cur.getColumnIndex("columnName")); // .... } } catch(IOException err) { Log.e(err); ToastBar.showErrorMessage("Error: " + err); } finally { Util.cleanup(db); Util.cleanup(cur); } Issue a Query Cleanup It’s crucial to cleanup properly. Otherwise on iOS the app might crash as the GC might cleanup before your code causing a thread conflict
  • 17. What did we learn? ✦When to choose file system storage and when to choose storage ✦How to implement data caching/offline mode using storage ✦The problems and value proposition of SQLite