5

I want to run the events of firebase on different thread. On the last version of firebase I had this code that did it

    Config firebaseConfig = new Config();
    firebaseConfig.setEventTarget(new EventTarget() {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        @Override
        public void postEvent(Runnable runnable) {
            executor.execute(runnable);
        }

        @Override
        public void shutdown() {
            executor.shutdown();
        }

        @Override
        public void restart() {

        }
    });
    Firebase.setDefaultConfig(firebaseConfig);

How can I do it in the new api? Their is a way or I have to implement it by my self? (create runnable of every function and run it in the executor)

Alon
  • 2,919
  • 6
  • 27
  • 47
  • i do not get it, but i want to give my thoughts, why not create a diff Thread and run all that code there. so when it gets back to the calling Thread-(which is your Background Thread) it is already in the background, then you can bring it to the MainUI thread – Elltz Aug 20 '16 at 09:39
  • firebase will runall the code in the event on the main thread... it does not matter where I write the code. – Alon Aug 20 '16 at 11:37

1 Answers1

16

The Firebase Database client performs all networking, disk I/O and other maintenance on a separate thread. It then surfaces the callbacks to your code on the main thread, so that you can interact with the UI.

In most situations you don't have to do anything special and can just let the Firebase client deal with the cross-threading handling. Only when you need to do some heavy work in your callback (e.g. onDataChange()) will you have to run that work off the main thread again. You can use the usual Android threading mechanisms for that.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I know that I can use the android threading mechanism, but On the last version It was possible to define it globally as the code in the question – Alon Aug 21 '16 at 02:38
  • That API doesn't exist in 3.x. It never was needed on Android. What are you trying to accomplish? – Frank van Puffelen Aug 21 '16 at 03:39
  • 1
    @FrankvanPuffelen I suppose, it would have been better if `onDataChange` run on a thread that it's parent has been called instead of running in MainThread. Isn't it? – musooff Nov 16 '18 at 01:25