Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

java - Refreshing multiple Realm instances at once?

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();
            }
        }
    }
}

And

SingletonBus.INSTANCE.postToMainThread(new RealmRefreshEvent());

@Subscribe
public void onRealmRefreshEvent(RealmRefreshEvent e) {
    this.realm.refresh();
}

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.) Is it bad practice / heavily resource-intensive to have multiple Realm instances open on the main thread?

2.) How resource-intensive is it to update multiple Realms on the same thread with a global refresh event?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

This also means that when you update 1 of you open instances, they all get updated automatically.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...