刷新多个领域实例一次? [英] Refreshing multiple Realm instances at once?
问题描述
我使用的是安装在每一个 presenter
这是一个留存片段
都有自己的服务器
实例。然而,这实际上意味着这些领域都在主线程中。
I'm using a setup in which every Presenter
that is a retained Fragment
has its own Realm
instance. However, this essentially means that these Realms are all on the main thread.
现在这也意味着,如果我想修改的境界,我要么需要做的主线程(这是正常的小数据集,但我真的不希望这样做大型数据集)或者我需要做它在后台线程,并在一次刷新每服务器实例(这是有可能用一个简单的事件事件总线)。
Now that also means, if I want to modify the Realm, I either need to do that on the main thread (which is okay for small data sets, but I don't really want to do that with large data sets), or I need to do it on a background thread, and refresh every Realm instance at once (which is possible with a simple event to the event bus).
public enum SingletonBus {
INSTANCE;
private static String TAG = SingletonBus.class.getSimpleName();
private Bus bus;
private boolean paused;
private final Vector<Object> eventQueueBuffer = new Vector<>();
private Handler handler = new Handler(Looper.getMainLooper());
private SingletonBus() {
this.bus = new Bus(ThreadEnforcer.ANY);
}
public <T> void postToSameThread(final T event) {
bus.post(event);
}
public <T> void postToMainThread(final T event) {
try {
if(paused) {
eventQueueBuffer.add(event);
} else {
handler.post(new Runnable() {
@Override
public void run() {
try {
bus.post(event);
} catch(Exception e) {
Log.e(TAG, "POST TO MAIN THREAD: BUS LEVEL");
throw e;
}
}
});
}
} catch(Exception e) {
Log.e(TAG, "POST TO MAIN THREAD: HANDLER LEVEL");
throw e;
}
}
public <T> void register(T subscriber) {
bus.register(subscriber);
}
public <T> void unregister(T subscriber) {
bus.unregister(subscriber);
}
public boolean isPaused() {
return paused;
}
public void setPaused(boolean paused) {
this.paused = paused;
if(!paused) {
Iterator<Object> eventIterator = eventQueueBuffer.iterator();
while(eventIterator.hasNext()) {
Object event = eventIterator.next();
postToMainThread(event);
eventIterator.remove();
}
}
}
}
和
SingletonBus.INSTANCE.postToMainThread(new RealmRefreshEvent());
@Subscribe
public void onRealmRefreshEvent(RealmRefreshEvent e) {
this.realm.refresh();
}
不过,假设我有大约5-7领域实例在主线程中打开(如每presenter有自己的开放领域,而他们不被破坏),我很担心性能和/或内存的使用情况。
But assuming I have about 5-7 realm instances open on the main thread (as every presenter has its own open realm while they are not destroyed), I'm concerned about performance and/or memory usage.
所以我想我有两个问题,
So I guess I have two questions,
1)它是不好的做法/巨资资源密集型有多个领域实例的主主题开放?
1.) Is it bad practice / heavily resource-intensive to have multiple Realm instances open on the main thread?
2。)如何资源密集型的是它更新在同一线程多个领域具有全球刷新
事件?
2.) How resource-intensive is it to update multiple Realms on the same thread with a global refresh
event?
推荐答案
领域使用一个ThreadLocal缓存内部公关。境界文件,因此它实际上是来电 Realm.getInstance()
在每一个活动/片段/ presenter你。以 Realm.getInstance第一次调用()
将花费一点点数据库已被打开,模式验证,但在那之后它只是花费高速缓存查找。
Realm uses a ThreadLocal cache internally pr. Realm file so it is practically free to call Realm.getInstance()
in every activity/fragment/presenter you have. The first call to Realm.getInstance()
will cost a little as database has to be opened and the schema verified, but after that it just cost a cache lookup.
缓存是引用计数这样的本土资源才会被释放后,所有实例已经关闭。这意味着它可以是有益的,以保持所述至少一个打开的实例周围尽可能长的时间。
The cache is reference counted so the native resources will only be freed after all instances has been closed. This means it can be beneficial to keep at least one open instance around as long as possible.
这也意味着,当你更新1的打开情况下,他们都得到自动更新。
This also means that when you update 1 of you open instances, they all get updated automatically.
这篇关于刷新多个领域实例一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!