Is performance in android degrading when interacting with a database storage often?
Is it better to load objects from DB and pass them around or access database frequently to get the objects?
I was thinking if the accessing the DB had more overhead due to instantiating objects from the result set each time.
I was thinking if the accessing the DB had more overhead due to instantiating objects from the result set each time.
Yes, it does, which is why if you are worried about performance, you should not use ORM tools to instantiate objects.
Is performance in android degrading when interacting with a database storage often?
Probably... you are reading from disk, which is slower than in-memory storage (which, hint, SQLite can do)
Is it better to load objects from DB and pass them around or access database frequently to get the objects?
Depends in what context you need actual class objects. If you store data in a database, then you should only query for that data when you need it, load it into an object, then do whatever calculation logic and save it. At least, that is my opinion on the matter. You shouldn't need to be serializing any objects between Activities primarily because you could lose state if you update an object in one Activity, pass it to another, then don't / forget to save it back to the database.
As google writes:
Note: Because they can be long-running, be sure that you call getWritableDatabase() or getReadableDatabase() in a background thread, such as with AsyncTask or IntentService.
which makes interacting with database out of main thread. So it can be not immadiate, so user will see some delay in app or fast appearing progress bar, or something. Especially when there will be big queries with 'union' and 'join'. But in most cases it is fast enough.
About your thinkig to access object due memory cache. It makes sense. Exact the same working with images provided by google here. So in your case db is disc cache other is same. You will need to provide some memory cache for your objects, but beware. If you working with huge number of them or each will be so heavy, you will need to provide not just simple wrapper to Map, that stores your objects, but something like LruCache.
So gathering all, you will recieve data from db, then store it in memory until app will need more memory.
Related
I want to keep some objects in memory for operations in all the activities of my app and I also want to store those objects when the app closes. Which is the most efficient way of doing this ? Some possibilities that I can think of are:
1) Keeping local copies of objects in all the activities, serialize them and pass them through intent.
2) Keep local copies of objects in all the activities, serialize them and do file read and write on activity resume and pause respectively.
3) Make them static variables but I don't know when to do the file read/write operations in that case? This approach may leak memory.
4) Use Application object and define my objects as variables in that object. Since it has a definite life cycle like activity, I can do read/write accordingly.
I recommend your approach number 2. The reason is that there is no such thing as "the app closes". Android tries to keep it in memory until the memory is needed for other purposes. The process of your app is then simply killed, you don't get any callbacks called.
Singletons or the Application object can be used to cache the objects if you are careful to load and store them as necessary. However, this also means, that the memory used for them is only reclaimed if the app process terminates.
It depends on your data. All approaches are good in some cases.
I think you have 2 options:
keep data in sqlite. It is easier then files and faster. When activity starts request required data from db and show it. (use files if you really want)
create a singletone class to store data. Data will be loaded in memory and you can access it very fast. When data changes save it to sqlite or file. google "share data between activities"
My app requires high data throughput. It receives an incoming data stream over bluetooth and has to parse it, scale it, display it, and store the data.
After reading up on different storage methods, I've decided to try to use Sqlite for data storage. I've read up and it seems to get the best write performance, I should use transactions.
But before I even go there, I'm wondering if the DB handler should just be in it's own thread. The BT data processing is already in a separate thread and works well. I see a lot of discussions about accessing sqlite from multiple threads, but I'm thinking I want one thread handling the DB connection and just use intents to get/store data..mostly just to disconnect the display from the data storage.
Will this increase my performance, or is it not worth it?
I'm guessing I'll be writing 20-50 rows a second with up to 19 fields.
Remember that SQLite is totally memory-resident. There is no disk latency time to slow up processing. As such, I would first try to keep the architecture as simple as possible to avoid creating any unnecessary overhead.
Why do we use the sqlite data base in android.I am developing an android application where the data is to be fetched from the server and do some data calculation and show it on the UI.
Is it good for me to fetch the data into the sqlite DB and update the UI on regular interval from the sqlite in evry 20 minutes or will it be good to sent the Http get request to the server and update the data from teh response on the UI.
I wanted to know which one will be better and why?Why to involve sqlite DB?
The data corresponds to some 40X40 table data on which some heavy mathematical processing is to be done and then displayed on the UI(similar to Stock market application) and data needs to be cleared after every 12 hours.
plz advice
Rgds,
Raul
It is good to use database in your case.
Pros:
If your application gets closed the in memory data will be lost, but after that you will be able to restore the state from the database if you have one
Especially for the case of complex calculations it is good to store the result once in the database and not recalculate it multiple times on demand
The database will untie your UI from the internet connection and thus you will be able to display results even if there is not internet connection
Using database you will be able to fetch the updated data from a background service, without impacting your UI
Organizing your data in database usually makes it a lot easier to manage all the application data.
Cons:
Adding database will require a bit of additional effort on your side
As you can see my list proves you SHOULD use database in your case. Maybe I am biased, but at least I provide you with things to consider.
It's really a design decision, SQLite offers a very robust way to organize and persist your data, you're only other options are to write to a file, or to save in SharedPrefs, both methods become a lot harder to manage once the size of your data begins to grow, as you must manually keep a list of objects and manage their names etc etc. 40 x 40 table data is large enough to justify using SQLite, even if you are dropping and recreating the table every 12 hours.
You might want to consider using an ORM library to make fetching and saving data from the DB simpler, ORMLite is good and compatible with Android
http://ormlite.com/
If your application relies heavily on an internet connection you don't need to buffer information in the database. However if you want to use the app where you have bad or no signal you might want to used cached values from the sqlite database.
With slow internet connection your application may be unresponsive so caching may be a good idea - but it doesn't necessarily be in the sqlite database. You should use the sqlite database for data that is required by the device frequently and that is irrelevant to your server component.
If the data is updated frequently but only while the application runs you might want to cache in the devices memory. I assume your app is not running all the time within the 12 hours but is called regularly instead to check something.
12hrs is a long time, so rather than leaving your data wander in RAM, i would suggest you to use database. Because you never know when you may need to read it again.
Otherwise, if your purpose is only to downloaded data, process it and display in activity, then you dont need to involve database because if your app is closed (due to user or low memory), in anyway your app will be downloading fresh data from server... am i right?
> update the UI on regular interval from the sqlite in evry 20 minutes
Dont expect your app to be open for such a long duration.
To precisely suggest to your case
Avoid DB
Fetch Data at app start or at appropriate time when app is opened
and save it in plain java objects.
Define Methods within it that perform operation in it.
Make map or list to save those POJO
Define Seprate Controller Classes within your project to update map of pojo at any
specific change to make fresh data available to UI.
I have a database from where i need to extract quite a lot of data.
Now i get that data when required, i.e. I have made a class that handles database interactions and whenever an activity requires data it will call that class for the data. So at a time an Activity only has the bare minimum amount of data in memory (i.e. the data that it is using). But everytime i change an activity i have to perform database access to fetch data for the new activity.
Method 2
As opposed to this i have this other alternative, in which i make an application object and then perform database access in the beginning and then store all the data that i would require (in all the activities) in the application object. Whenever i need the data, i refer to the application object. The downside of this that i will be holding too much extra data that i am not using at a given instant.
Which of the above 2 approaches is better?
Thank you in advance.
It depends on your requirements and their priorities. If the time required for solution 2 is too long for you to accept then optimize (e.g. by using method 2, but in general I would advise against storing potentially all of your database in memory ... assuming the amount of memory will suffice).
Did you try solution 1. If the problem is only to read the data from database, it should not take too long to load the data for one activity. If complex calculations are involved you might be pressed to optimize. But don't optimize just in case!
I prefer the first approach because making a call to database is not costly until and unless it is being accessed by multiple applications.
I have a small app that records messages and stores them in an object which implements Parcelable. In the app I have a LOT of messages (about 2000) and it takes a considerable amount of time passing between Activities through intent.putParcelableArrayListExtra
Is this not the correct usage of Intents+Parcelable? I've been really wanting to avoid SQLite, but I suppose I'll get my hands dirty if its absolutely necessary.
You should definitely persist your messages in a database. If you want to avoid all the SQLite hassle check db4o, painless object persistence in your apps using an OODB.