3

getFromLocation method in Geocoder is showing Inappropriate blocking method call warning even though I am calling it on non-main thread as shown below.

I looked other posts including How to make "inappropriate blocking method call" appropriate? and the answer is that it's false positive warning. I want to make sure if the answer is the same for Geocoder. Is it safe to ignore this warning?

Geocoder to get address with Kotlin coroutine:

    private suspend fun getAddress(): String = withContext(Dispatchers.IO) {
        val location = (activity as MainActivity).currentLoc
        location ?: return@withContext "location not found, try again"

        // gets full address given
        return@withContext try {
            Geocoder(requireContext(), Locale.getDefault()).getFromLocation(
                location.latitude, location.longitude, 1
            ).first().getAddressLine(0).removeSuffix(", USA")

        } catch (ioException: IOException) {
            Log.e(TAG, "getAddress error -------------------------------- ", ioException)
            "location not found, try again"
        }
    }

Calling getAddress() method:

  lifecycleScope.launch {
        val result = getAddress()
leo
  • 113
  • 3
  • 11

0 Answers0