[英]Android MVP Architecture and Realm - How to Avoid Passing Context among the MVP layers?
I have been learning Android MVP for awhile, in most of my Application, I find that it is not so practical to passing the Context Data among the MVP Layer for testability purposes. 我学习Android MVP已有一段时间了,在我的大多数应用程序中,我发现出于可测试性的目的,在MVP层之间传递上下文数据并不那么实际。
However, for some cases, it is required to do so, for example, in order to access Realm database, I would need the Context Data for performing this implementation: 但是,在某些情况下,需要这样做,例如,为了访问Realm数据库,我将需要Context Data来执行此实现:
Realm Implementation 领域实施
Realm.init(mainContext)
val config = RealmConfiguration.Builder()
.name(mainContext.getString(R.string.accountRealm))
.build()
realm = Realm.getInstance(config)
Only that I can perform the CRUD functionality of Realm. 只有这样我才能执行Realm的CRUD功能。
Because of that I have to always pass the Context Data from View Layer to Model Layer which I believe which is not so practical. 因此,我必须始终将上下文数据从视图层传递到模型层,我认为这不太实用。
My Question: 我的问题:
Is there any other way for me to implement the Realm functionality without the need to use the Context Data ? 是否有其他不需要使用Context Data来实现Realm功能的方法? How should I do it in the right way? 我应该如何以正确的方式来做?
Is it okay/acceptable to keep passing Context Data or other similar android specific code among the MVP layer? 在MVP层之间继续传递上下文数据或其他类似的android特定代码是否可以/可以接受? Like for this Realm case, is it consider as an 'Acceptable Trade-off'? 像此Realm案例一样,它是否被视为“可接受的折衷”?
There two flaws (if I may call so) in the code snippet. 代码段中有两个缺陷(如果可以的话)。
First. 第一。 Realm.init(mainContext)
should be done in onCreate()
of Application, just once. Realm.init(mainContext)
应该在Application的onCreate()
中完成一次。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
}
}
Then add MyApplication
to your manifest.xml
file as below. 然后如下所示将MyApplication
添加到manifest.xml
文件中。
<application
android:name=".MyApplication"
<!--rest of properties-->
/>
Second. 第二。 It's usually good idea to read static strings from resources, but not in all case. 从资源中读取静态字符串通常是一个好主意,但并非在所有情况下都如此。 This case, for example, is an exception. 例如,这种情况是例外。
Create a java class to hold your database name as belowe: 创建一个Java类来保存您的数据库名称,如下所示:
public class AppStatics{
public final static REALM_DATABASE_NAME = "myrealm.realm";
}
Then simply take database name from this class to set name for Realm database. 然后只需从此类中获取数据库名称即可为Realm数据库设置名称。 . 。 You don't have to configure Realm everytime. 您不必每次都配置Realm。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration.Builder()
.name(mainContext.getString(R.string.accountRealm))
.build()
Realm.setDefaultConfiguration(config);
}
}
Now you can call instance of Realm by calling Realm realm = Realm.getDefaultInstance();
现在您可以通过调用Realm realm = Realm.getDefaultInstance();
来调用Realm realm = Realm.getDefaultInstance();
实例Realm realm = Realm.getDefaultInstance();
in your Model (data operations module of MVP) 在您的模型中(MVP的数据操作模块)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.