Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting data from a database vs getting data from a hash map

In my android *project* I have to keep track of product details of certain number of products. All the data on these products are store in a SQLite database. I can use select and update in SQLite to perform keep track of product objects. So I can store product details when ever they are changed. Also I can load all the products into a hash map or such a data structure at the beginning and keep track of those product objects. what matters to me is out of above both which one is more efficient and productive. Can someone help me. Thank you!

like image 642
Samantha Withanage Avatar asked Dec 06 '13 06:12

Samantha Withanage


1 Answers

This depends on the number of products. A HashMap resides in RAM, a database resides on the disk.

This also depends on the number of queries per second and the nature of the queries. Database developers have put a lot of effort to support indexing and filtering; if you need that, reuse is better than re-inventing.

Whatever approach you choose, you must remember that an Android application process may be killed at any moment (e.g. when memory is needed for another process), and your code is guaranteed only to receive onPause() (and not onDestroy() or onStop(), see the activity life cycle). If necessary, the application will be restarted later, but all data kept in RAM (and not saved) will be of course lost. Therefore, in either onPause() or in onSaveInstanceState() (what bundles are for) you must save the application state. In your case this may mean having to save the HashMap and all auxiliary data structures.

OTOH, if the number of products is small (and is expected to remain small), a database can be an overkill; if you just need to choose one of 10 items, writing a loop is faster than doing all the database support.

One more important note: an Activity is a Controller from the MVC viewpoint (and as to the View, you usually create and XML and reuse the existing framework classes, although you could program custom controls). An Activity is re-created each time the device orientation changes and in some other cases. But your data belong to the Model part. They must survive the orientation change. So they must not be owned by an Activity.

like image 80
18446744073709551615 Avatar answered Sep 23 '22 22:09

18446744073709551615