Should we interact with a db storage often in android?

风格不统一 提交于 2019-12-24 09:41:07

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/38465252/should-we-interact-with-a-db-storage-often-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!