Jim
Jim

Reputation: 19562

Should we interact with a db storage often in android?

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.

Upvotes: 1

Views: 132

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191728

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.

Upvotes: 2

Mikhail Spitsin
Mikhail Spitsin

Reputation: 2608

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.

Upvotes: 0

Related Questions