Home > other >  is this good approach to fetch data every n second by hitting server or rest API in android
is this good approach to fetch data every n second by hitting server or rest API in android

Time:01-11

I am developing an application in which I want to fetch data after every 30 seconds so i am using this line of code:

 handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!Constants.IsCafeNewOrderAlert){
                    new_order_api();
                }
                reset_api();
            }
        },30000);

it works fine in my scenario but I just want to know is this approach is good or it can be done with some better code or approach.

thank you in advance.

CodePudding user response:

there are some ways that are shorter, however they are a little harder to implement. I don't think it even matters if it is one line longer than something else unless it's some challenge.

handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!Constants.IsCafeNewOrderAlert){
                    new_order_api();
                }
                reset_api();
            }
        },30000);

What you have is perfectly fine and the easiest and one of the shortest ways to implement. Good luck with your app! And your welcome! If you find this helpful, please accept my answer as I need some reputation.

CodePudding user response:

From battery point of view this approach is very expensive (draining your battery) because the radio has no chance to shut off to save energy.

  •  Tags:  
  • Related