问题描述
我想在的ContentProvider
注入一个单身 SqliteOpenHelper
。然而,似乎正在创建应用程序实例之前的的ContentProvider
实例正在修建( getApplicationContext()
返回null )。我什么时候可以注入数据库?我试着在构造函数和的的onCreate()
方法的ContentProvider
。
I want to inject a singleton SqliteOpenHelper
in a ContentProvider
. However, it seems that the ContentProvider
instance is being built before the Application instance is being created (getApplicationContext()
returns null). When can I inject the database? I've tried in the constructor and in the onCreate()
method of the ContentProvider
.
推荐答案
我面临着同样的问题,不得不推迟注射,直到所需的数据库。你也许可以使用匕首的来达到同样的效果。
I faced the same issue and had to defer injection until the database was needed. You might be able to use Dagger's lazy injection to achieve the same effect.
从内容提供商的
您应该推迟平凡的初始化(如打开,升级和扫描数据库),直到内容提供商使用
显然,这个建议不能被忽视。 提供了一个例子使用SQLiteOpenHelper与内容提供商
Apparently this suggestion cannot be disregarded. Implementing the onCreate() method provides an example using an SQLiteOpenHelper with a content provider.
这篇关于数据库注射用匕首ContentProvider的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!