title: Work manager android: UI is blocking while writing the file to local storage
body: <p>This is my worker class:</p>
  
  <pre><code> class CoroutineDownloadJSONWorker(val context: Context, params: WorkerParameters) :
          CoroutineWorker(context, params) {
          private var notificationManager: NotificationManager? = null
  
          companion object {
              private const val WORK_NAME = "CoroutineDownloadJSONWorker"
              const val PARAM_PROGRESS = "Progress"
  
              fun run(context: Context): LiveData&lt;WorkInfo&gt; {
                  val work = OneTimeWorkRequestBuilder&lt;CoroutineDownloadJSONWorker&gt;()
                      .build()
  
                  val manager = WorkManager.getInstance(context)
                  manager.enqueueUniqueWork(WORK_NAME, ExistingWorkPolicy.REPLACE, work)
                  return manager.getWorkInfoByIdLiveData(work.id)
              }
  
              fun stop(context: Context) {
                  //  Timber.d("stop")
                  val manager = WorkManager.getInstance(context)
                  manager.cancelUniqueWork(WORK_NAME)
              }
          }
  
          override suspend fun doWork(): Result {
              notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
              val progress = "Starting Download"
              setForegroundAsync(createForegroundInfo(progress))
              val service = Api.getClient.create(ApiService::class.java)
              val call = service.getMapList().execute()
              if (call.isSuccessful) {
                  val mapListResponse = call.body()
                  if (!mapListResponse!!.result.isNullOrEmpty()) {
                      val fileUrl = mapListResponse.result!![mapListResponse.result.size - 1].id
                      // getDownloadUrl(fileUrl!!)
                      val mapUrls = service.getDownloadMapURL(fileUrl!!).execute()
                      if (mapUrls.isSuccessful) {
                          val mapUrlsResponse = mapUrls.body()
                          if (!mapUrlsResponse!!.json_link.isNullOrEmpty()) {
                              val fileUrl = mapUrlsResponse.json_link
                              val map = service.downloadLatestMap(fileUrl)!!.execute()
                              if (map.isSuccessful) {
                                  if (map.body() != null) {
                                    //  getMapFromUrl(map.body())
                                      try {
                                          val futureStudioIconFile =
                                              File(context.getExternalFilesDir(null), "euthopia.json")
                                          var inputStream: InputStream? = null;
                                          var outputStream: OutputStream? = null;
                                          try {
                                              val buffer = ByteArray(4096)
                                              var fileSizeDownloaded:Long = 0;
                                              inputStream = (map.body()!!.byteStream())
                                              outputStream = FileOutputStream(futureStudioIconFile);
                                              while (true) {
                                                  val read = inputStream.read(buffer);
                                                  if (read == -1) {
                                                      break;
                                                  }
                                                  outputStream.write(buffer, 0, read);
                                                  fileSizeDownloaded += read
                                                  setForeground(createForegroundInfo(AppUtils.getBytesString(fileSizeDownloaded)))
  
                                              }
                                              outputStream.flush();
                                              saveToDB()
                                          } catch (e: IOException) {
                                          } finally {
                                              if (inputStream != null) {
                                                  inputStream.close();
                                              }
                                              if (outputStream != null) {
                                                  outputStream.close();
                                              }
                                          }
                                      } catch (e: IOException) {
                                      }
  
                                  }
                              }
  
                          }
  
                      }
                  }
  
              }
  
  
              return Result.success()
          }
  
  
          private fun createForegroundInfo(progress: String): ForegroundInfo {
              val context = context
              val id = context.getString(R.string.notification_channel_id)
              val title = context.getString(R.string.notification_title)
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
              }
              val notification: Notification = NotificationCompat.Builder(context, id)
                  .setContentTitle(title)
                  .setTicker(title)
                  .setContentText(progress)
                  .setSmallIcon(R.drawable.logo)
                  .setOngoing(true) // Add the cancel action to the notification which can
                .build()
              return ForegroundInfo(notification)
          }
  
      }
  </code></pre>
  
  <p>I am following this tutorial <a href="https://codelabs.developers.google.com/codelabs/android-workmanager/#0" rel="nofollow noreferrer">work manager codelab</a>. I am trying to download the JSON file from the server and saving it into local storage. I am not sure why the UI is blocking. while writing the file into local storage in  <code>doWork()</code> function of <code>CoroutineWorker</code>. I am showing the progress on a notification while writing the file. The app getting stuck.</p>