RSS

Going Offline With GWT and Gears

25 Sep

Why?

Web apps are great …if you can connect

•Planes

•Trains

•Automobiles

•Users may not want their data in the cloud

•Increase response time for online apps

How?

GWT – Google Web Toolkit

Google Gears

s

  • Browser Plugin (FireFox, Internet Explorer)
  • http://gears.google.com
  • Features:
    LocalServer (“programmable cache”)
    SQLite Database
    Worker Threads for JavaScript

Take Your App Offline

  1. Manifest file of your app’s resources
  2. Download resources
  3. Create database schema
  4. Go

Manifest File

Load Resources

LocalServer localServer = new LocalServer();ManagedResourceStore managedRS =
localServer.createManagedResourceStore(“CompanyApp”);
managedRS.setManifestURL(“http://company.com/manifest.json”);
managedR.checkForUpdate();
new Timer() {
public void run() {
switch (managedResourceStore.getUpdateStatus()) {
case ManagedResourceStore.UPDATE_OK:
statusLabel.setText(“Ready for offline access”);
break;
case ManagedResourceStore.UPDATE_CHECKING:
case ManagedResourceStore.UPDATE_DOWNLOADING:
schedule(500);
break;
case ManagedResourceStore.UPDATE_FAILED:
statusLabel.setText(“Unable to go offline);
break;
} } }.schedule(500);

Gears Database

Create Database

  • “create table if not exists person (id integer, first_name text, last_name text)”
  • Datatypes: Integer, Real, Text, Blob
  • Constraints: “primary key”, “not null”, “unique”, etc.

private Database m_database = null;try {
m_database = new Database(“Test”);
ResultSet rs = m_database.execute(“create table…”);
rs.close();
} // try
catch (Exception e) {
// Gears not installed
} // catch

Queries

String sql = “select id, first_name, last_name  from person”;
ResultSet rs = m_database.execute(sql);
ArrayList results = new ArrayList();
while (rs.isValidRow()) {
PersonBean person = new PersonBean();
person.setID(rs.getFieldAsInt(0));
person.setFirstName(rs.getFieldAsString(1));
person.setLastName(rs.getFieldAsString(2));
results.add(person);
rs.next();
} // while
rs.close();

Insert, Update

String args[] = new String[3];
args[0] = Integer.toString(person.getID());
args[1] = person.getFirstName();
args[2] = person.getLastName();ResultSet rs = m_database.execute(“insert into person
(id, first_name, last_name) values (?,?,?)”, args);
rs.close();args = new String[3];
args[0] = person.getFirstName();
args[1] = person.getLastName();args[2] = Integer.toString(person.getID());
rs = m_database.execute(“update person set
first_name=?, last_name=? where id=?)”, args);
rs.close();

Demo Source Code

Syncing Issues

  • Need GUIDs
  • Need timestamps (SQLite has no Date)
  • Need a strategy:
    Last one wins
    Lock / Check out
    Let user decide

Conclusion

Google Gears allows web applications to run offline
Google Web Toolkit makes it easy to program Gears

Advertisements
 
Leave a comment

Posted by on September 25, 2008 in Google Gears

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: