I am developing an app that at startup sets up a database cursor to display values in a listview. During the course of the app running data in the database gets updated and i am updating the cursor constantly.
It’s kinda working but sometimes the cursor lags behind the database updates, thus displaying old data in the list until after some seconds.
Would it be more efficient if i load all the data (approx 10-20 sets of approx 20 strings and ints each) into an array of objects at startup and write to database only on close?
What is the expense of database queries versus creating arrays of objects?
Thanks for help!
It depends on your definition of “expensive”. If you’re referring to bandwidth, constant queries is more expensive. Each query to a remote database occurs on an order of milliseconds while a query to an object in memory is on an order of nanoseconds.
If “expensive” means immediate processing speed discounting network latency, then loading them all into memory will be more expensive because the system is sifting through more information at the time of action.
In your case, since you’re dealing with android, I’d say your best bet is a combination of the two. You’ll want to load as much data as you’ll conceivably need to work with at any given time. You may want to look at possibly creating adjacency lists for data if that will help with the data retrieval. With a good adjacency list system, you can query data you will soon need while working on data you have now (this helps reduce the affect of network latency).
As for the updating portion, unless you’re going to queue your changes and upload them asynchronously, you can’t really avoid that latency. It’s just how the network operates. However, if you CAN work asynchronously with the interface, you can queue the change up and instruct the update to physically occur at the database level while the user continues on to the next task.