java - 更新托管对象字段的速度很慢
问题描述
我有一个相当简单的托管领域对象RealmAlertItem
,由一些字符串和浮点数组成。我有一个showAlertNotification()
随后被调用的函数(从网络外部触发),它依赖于上一次执行在下一次调用之前完全完成的事实。setHigh()
在这个函数中,我只更改一个浮点字段(见setLow()
下文)。如果两者都是 0,我删除该对象。
但是,似乎 Realm 无法及时更新对象,并且在第二次调用该函数时,该对象仍然具有旧值high
和low
值。
这是logcat:
08:47:04.180 D/debug: showAlertNotification(), guid: 58dfe381-b615-4ce0-beaf-5abb9b0733ac
08:47:04.185 D/debug: high 10000.0 , low 10800.0
08:47:04.205 D/debug: b // here we call RealmInstance.getThreadRealm().executeTransaction(realm1 -> alert.setHigh(0));
08:47:04.214 /debug: showAlertNotification(), guid: 58dfe381-b615-4ce0-beaf-5abb9b0733ac
08:47:04.215 /debug: high 10000.0 , low 10800.0 // here high should be 0!
08:47:04.224 /debug: d
如您所见,第二次调用在第一次调用之后发生得非常快(大约 30 毫秒后)。
这是代码:
private void showAlertNotification(RemoteMessage remoteMessage, String coinSlug, String coinName, Bitmap logo) {
Log.d("debug", "showAlertNotification(), guid: " + remoteMessage.getData().get("guid"));
RealmAlertItem alert = RealmInstance.getThreadRealm().where(RealmAlertItem.class).equalTo("guid", remoteMessage.getData().get("guid")).findFirst();
if(alert == null)
return;
... some work
// disable/delete this alert
if(!alert.isRepeating()) {
if (isHighLimit) {
if (alert.getLow() == 0) {
Log.d("debug", "a");
RealmInstance.getThreadRealm().executeTransaction(realm1 -> alert.deleteFromRealm());
} else {
Log.d("debug", "b");
RealmInstance.getThreadRealm().executeTransaction(realm1 -> alert.setHigh(0));
}
} else {
if (alert.getHigh() == 0) {
Log.d("debug", "c");
RealmInstance.getThreadRealm().executeTransaction(realm1 -> alert.deleteFromRealm());
} else {
Log.d("debug", "d");
RealmInstance.getThreadRealm().executeTransaction(realm1 -> alert.setLow(0));
}
}
}
Intent intent = new Intent("notifyAlertsChanged");
LocalBroadcastManager.getInstance(MainApplication.getAppContext()).sendBroadcast(intent);
}
如果需要,这是我的 RealmInstance Singleton:
public class RealmInstance {
private Realm realm;
private static RealmInstance realmInstance;
public static RealmInstance getInstance() {
if (realmInstance == null) {
realmInstance = new RealmInstance();
}
return realmInstance;
}
private RealmInstance() {
try {
realm = Realm.getDefaultInstance();
} catch (IllegalStateException err) {
init(MainApplication.getAppContext());
realm = Realm.getDefaultInstance();
}
}
public Realm getRealm() {
return realm;
}
public static Realm getThreadRealm() {
return Realm.getDefaultInstance();
}
public static void init(Context context) {
Realm.init(context);
RealmConfiguration config = new RealmConfiguration
.Builder()
.schemaVersion(13)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
}
}
我该如何解决这个问题?
解决方案
您需要在您的 中初始化您的领域配置Application.java
,并在需要时调用Realm.getDefaultInstance()
它(即摆脱您的单例类)。
您还需要在使用后关闭领域实例,否则会产生副作用,就像您正在试验一样,稍后您将遇到性能问题。
推荐阅读
- javascript - HTML | 让表单向用户输入的电子邮件发送电子邮件
- python - 将 X 值的数据集特征读入它们自己的数组列,将 y(目标)读入其数组但在主数组内
- maven - 如何使用 maven 删除 lib 中不需要的 jar
- flutter - 颤动:为什么除非我单击文本字段,否则提示文本不会出现?
- html - 在 ASP.NET 中添加背景图像以覆盖整个屏幕
- flutter - 带控制器的 SingleChildScrollView 第一次不滚动
- python - 将数据从 SQL 读取到 Python 作为数据框
- c# - 通过 Webforms Page .Net Frameworks 调用 API WEB
- arduino - 将 char* 转换为字节数组的 Arduino 函数
- javascript - React 中的 onMouseEnter 事件无法正常工作