0

In my foreground service, I need to connect to my backend to download the latest data once every 3 minutes and display the data on a notification. After a few minutes of googling, it seems using 'ScheduledThreadPoolExecutor' would be the best. But I'm still not clear on what to put for the pool size.

What's the consideration that I should take when deciding the pool size? Basically what my foreground service will be doing is:

  1. Connect to an API every 180 seconds
  2. Save the data in db using Room
  3. Display notification Below is some of my code:
class BusinessService: Service() {
    private var job: Job? = null
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val notificationIntent = Intent(this, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)    
        val exec = ScheduledThreadPoolExecutor(3)
            val delay: Long = 180
    
            exec.scheduleWithFixedDelay(object : Runnable {
                override fun run() {
                    CoroutineScope(Dispatchers.IO).launch {
                        val params = HashMap<String, String>()
                        params["email"] = "test@gmail.com"
                        val sellerLoginResult = EarthlingsApi.retrofitService.sellerLogin(params)    
                        when (sellerLoginResult) {
                            is NetworkResponse.Success -> {
                                Timber.d(sellerLoginResult.body.msg)
                            }
                        }
                        val channelId =
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                createNotificationChannel("my_service", "My Background Service")
                            } else {
                                ""
                            }
                        val notification = NotificationCompat.Builder(myContext, channelId)
                            .setContentTitle(getString(R.string.Business_Notification_Header,name))
                            .setContentText(getString(R.string.Business_Notification_Content,"7"))
                            .setSmallIcon(R.mipmap.ic_launcher_round)
                            .setContentIntent(pendingIntent)
                            .build()
                        startForeground(111, notification)
                    }
                }
            }, 0, delay, TimeUnit.SECONDS)
            return START_STICKY
        }
    }
imin
  • 4,504
  • 13
  • 56
  • 103

1 Answers1

1

Hay imin!

Your pool size is just the amount of threads that exist within the pool at any given time. I think 2 threads would do the job for you just fine considering that downloading/waiting for data can exist on one thread and you send that to another to display as a notification. If you're noticing performance hitches maybe consider trying 1 because you have a specific order to your events and they won't be happening at the same time unless the API takes 180 seconds to deliver the notification data.

I know within an app you would use the dedicated UI thread to move any data from a background thread to display on screen but I'm not sure if that's an option for notifications.

Hopefully, this helped!