开发者

open a database using Android and MVC

I'm writing an Android application, a dictionary just for fun. I've decided to use MVC as architectural pattern. I need to do many queries because I want to c开发者_Go百科reate an istant searching like Google does: Every time the user add a letter to the EditText I want do query the database.

I'm wondering what is the best solution if opening the database every time or keeping it always open. The former could be better because the database is open only when is strictly necessary, the latter should be more performant.

There is another way. Because I'm using MVC is the view that will query the database. So I can create in the Model part a public method to open the database when necessary and close it when the user has found the word. But could this be a good solution? Two of the most important function of the database (open and close) will be exposed at the view part. This doesn't sound much correct: it hasn't got any information hiding.

What do you think about this logical and perfomant problem?


If we're talking purely on the theoretical pattern design, the truth is that the Model probably shouldn't even expose the fact that it's getting it's data from a database. You can easily keep track internally in the model of whether the database is open or not and open it when necessary.

On the performance aspect of the question, when it comes to questions like this, the only way to get a good answer is to measure. It could be that keeping the database open won't have any ill effects and you won't have any problems just keeping it open. That will be the easiest and fastest solution more than likely. However, it could be that keeping it open might take up too much memory, or keep the CPU busy, or some other side effect.

As I mentioned, the only way to really have a definitive answer is to measure. You can use DDMS, amongst other profiling tools.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜