使用相同的sqlite进行三个活动 [英] Using same sqlite for three activities

查看:68
本文介绍了使用相同的sqlite进行三个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用同时打开的三个活动.所有活动都是来自sqlite的追溯数据.从活动a-> b或从b-> c进入时,我不会关闭或重新打开连接.我只是在活动销毁时处理我的数据库.

I am using three activities which are opened at the same time. All activities are retreive data from sqlite. I don't close or re open my connection when i am going from activity a->b or from b->c. I just dispose my db when activity is destroying.

活动A

SqliteConnection db;

OnCreate method
db = new SqliteConnection(mypath);

OnDestroy
db.Dispose();
db=null;

Onbuttonclick
startActivity(new Intent(this, ActivityB));

我从活动b-> c进入时,正在运行相同的代码.在同一个活动中,我经常使用sqlite.

Same code is running when i am going from activity b->c. Inside the same activity i use sqlite plenty of times.

这是一个好习惯吗?使用后我应该立即处置连接吗?还是我应该在暂停时关闭连接并在恢复时重新打开?还是可以将相同的打开的连接传递给下一个活动?哪种方法最好?

Is this a good practice? Should i dispose my connection immediatelly after a use? Or should i close my connection on pause and reopen on resume? Or can i pass the same opened connection to the next activity? Which is the best approach?

问题已修改

class databaseHelper
{
      private static SqliteConnection db;

      public static SqliteConnection openDatabase(Context context)
   {
         if(db==null)
             db = new SqliteConnection(mypath);

           return db;

   }

}

在我创建活动中

  databaseHelper.openDatabase(this).myquery....

推荐答案

我不支持Java或xamarin.这是Kotlin代码,非常不言自明.

I don`t roll with Java nor xamarin. Here is a Kotlin code, it is pretty self-explanatory.

class DatabaseHelper {    //Public class

companion object {   ///This is equiavalent to java static.

    private var instance: YourDatabase? = null


    fun getDbInstance(context: Context): YourDatabase?  //This functions returns the initialized DB instance.
    {
        if(instance == null)
            instance = YourDatabase(context)   // initializing the DB only one time

        return instance
    }

   }
}

只需创建一个公共类并将其命名为"DatabaseHelper"即可.在类内部,创建一个数据库类型的静态变量.创建一个返回静态变量的公共函数.在函数内部,首先,检查静态实例是否为null,是否为null,然后使用数据库实例对其进行初始化.这样,当您需要使用数据库实例时,只需访问静态函数,为其提供上下文,它将返回您初始化的数据库实例.

Just create a public class and name it for example "DatabaseHelper". Inside the class, create one static variable of your database type. Create a public function that returns the static variable. Inside the function, first, check if the static instance is null and if it is null, then initialize it with your database instance. This way, when you need to use your database instance, just, access the static function, provide it with the context and it will return you the initialized database instance.

在科特林

DatabaseHelper.getDbInstance(this).yourDbFunction()

更新

自从这个答案开始以来,我想提出对以前解决方案的改进建议.与其传递活动上下文来初始化数据库,不如使用应用程序上下文.如果将活动上下文提供给静态数据库实例,则会发生内存泄漏,因为数据库实例持有对该活动的 strong引用,而该活动将有资格进行垃圾回收.

UPDATE

Since this answer took off, I would like to suggest improvements to my previous solution. Instead of passing a context of activity to initialize the database, use application context. If you give an activity context to the static database instance, a memory leak will occur because the database instance holds a strong reference to the activity and the activity will NOT be eligible for garbage collection.

正确用法:

val myDb = MyDb(applicationContext)

这篇关于使用相同的sqlite进行三个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆