My Android App has a products table in an SQLite database that comes pre-populated when the user installs it. The table can be updated from an Azure web service.
Instead of returning only the updated records from the web service and populating the SQLite table with the updated records, I want to simply return the 2900 records to the App. This is because most of the products will have changed. On opening the App, the products table is deleted using an SQL query and the ksoap2 response is sent to Product objects which are inserted into the database.
While this update to the products table is happening, I would like the user to be able to use the App without disruption. If the Products table has been deleted, then they cannot operate the App properly.
What are my options? Could I populate a temporary Product table and when the service has finished populating it, I could copy it to the ‘live’ Product table. Or could I completely delete the Product table and rename the temporary table to “Product”?
Or am I going about this completely wrong. Any advice would be most appreciated.
Code extract below:
try {
productDatasource.open();
productDatasource.deleteProductTable();
productDatasource.close();
ArrayList<Product> arrProduct = new ArrayList<Product>();
SoapObject request = new SoapObject(NAMESPACE, PRODUCT_METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
System.out.println("startit");
HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
ht.call(SOAP_ACTION_PRODUCT, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
productDatasource.open();
productDatasource.deleteProductTable();
Product[] products = new Product[response.getPropertyCount()];
for (int i = 0; i < products.length; i++) {
SoapObject prodObj = (SoapObject)response.getProperty(i);
Product product = new Product();
product.setProductID(Integer.parseInt(prodObj.getProperty(10).toString()));
product.setProductName(prodObj.getProperty(11).toString());
product.setFKCategoryID(Integer.parseInt(prodObj.getProperty(5).toString()));
product.setFKSubCategoryID(Integer.parseInt(prodObj.getProperty(13).toString()));
product.setFKBrandID(Integer.parseInt(prodObj.getProperty(2).toString()));
productDatasource.createProduct(product);
}
Use a ContentProvider together with a CursorLoader so that your UI remains fluid while the content is updating. Use BulkInsert to add rows faster. If you can just update records instead of replacing them, then add a column that lets you mark the record as needing update. For information on getting started with SQL databases on Android, see http://developer.android.com/guide/topics/data/data-storage.html#db and http://developer.android.com/training/basics/data-storage/databases.html.