CoroutineWorker locks UI only when starting and ending job

18 Views Asked by At

The UI freezes for a few milliseconds only when starting and ending work. After that I have no problems, when I close the application it still works and everything works perfectly. The only detail or issue is that only when starting and ending does it block the UI for a very short period of time as mentioned above.



class MediaManagementWorker(context: Context, params: WorkerParameters) :

    CoroutineWorker(context, params) {

    override suspend fun doWork(): Result {

        return withContext(Dispatchers.IO) {

            try {

                setForeground(createForegroundInfo(type))

            } catch (i: IllegalStateException) {

                return@withContext Result.failure()

            }

            val result = mediaManagementWorker(media, albumId, type)

            if (result) return@withContext Result.success() else Result.failure()

        }

    }

    private suspend fun mediaManagementWorker(

        mediaId: Array<String>,

        albumId: Long,

        type: Boolean

    ): Boolean {

    

        val item = ....

        if (type) {

            copyMediaStore(applicationContext, item)

        } else {

            moveMediaStore(applicationContext, item)

        }

        return true

    }

    

    

    suspend fun copyMediaStore(context: Context, copyMove: CopyMove): String {


    return withContext(Dispatchers.IO){


        if (!isMountedVolume(context, copyMove.volumeName)) {


            return@withContext "volume not mounted"


        }





        val values = ContentValues().apply {


            put(MediaStore.MediaColumns.RELATIVE_PATH, copyMove.newRelative)


            put(MediaStore.MediaColumns.DISPLAY_NAME, copyMove.displayName)


            put(MediaStore.MediaColumns.MIME_TYPE, copyMove.mime)


        }





        val contentResolver = context.contentResolver


        val output: Uri = contentResolver.insert(copyMove.uri, values) ?: return@withContext "Output Uri is Null"





        var inputStream: InputStream? = null


        var outputStream: OutputStream? = null


        try {


            inputStream = contentResolver.openInputStream(copyMove.uri)


                ?: return@withContext "FileNotFoundException - inputStream"


            outputStream = contentResolver.openOutputStream(output)


                ?: return@withContext "FileNotFoundException - outputStream"





            val buffer = ByteArray(DEFAULT_BUFFER_SIZE)


            //val buffer = ByteArray(1024)


            var bytes = inputStream.read(buffer)


            var progress = 0L





            while (bytes >= 0) {


                outputStream.write(buffer, 0, bytes)


                progress += bytes


                bytes = inputStream.read(buffer)





                Log.d("tag_file_management", "management...$bytes")


            }


            outputStream.flush()


            inputStream.close()


            outputStream.close()





        } catch (e: Exception) {


            Log.d("tag_file_management", "error:$e")


        } finally {


            outputStream?.flush()


            inputStream?.close()


            outputStream?.close()


            Log.d("tag_file_management", "finally...")





        }


        Log.d("tag_file_management", "finished...")


        return@withContext "finish"


    }


}

I am copying a file and the only thing that has not blocked the UI at the moment is using a (ByteArray(1024)). But it still blocks the UI when starting and ending. Please take a look at my code and help me overcome that detail or error. Thank you very much

val buffer = ByteArray(DEFAULT_BUFFER_SIZE)


            //val buffer = ByteArray(1024)


            var bytes = inputStream.read(buffer)
0

There are 0 best solutions below