Ramesh Jaya
Ramesh Jaya

Reputation: 671

Pausing an ongoing iterative process for user interaction in Kotlin-Android

I've been working on a function that going to print given documents via a Bluetooth printer. After sometime, the client wanted to print multiple copies of each document and I've modified my existing code for that purpose.

> var count = 5
>         for ((key, value) in printHashMap) {
>       if (key == "BILL_PRINT"){
>     
>                     if (type == "0") {
>                         
>     
>                         for(i in 1 until count) {
>                             
>                                 var dialog = SweetAlertDialog(context, SweetAlertDialog.BUTTON_CONFIRM)
>                                 dialog.setTitleText("Printing Copy ($i)")
>                                         .showCancelButton(false)
>                                         .setCancelText("No")
>                                         .setCancelClickListener { sDialog ->
>                                             sDialog.dismiss()
>                                             //need to break the loop exit to the outer For-Loop
>     
>                                         }
>                                         .setConfirmText("Yes")
>                                         .setConfirmClickListener { sDialog ->
>                                             sDialog.dismissWithAnimation()                                       
>     
>                                             ioScope.launch {
>                                                 runBlocking {
>     
>                                                     if (printingDone(value)) {
>     
>                                                         Log.d("Printer", "Printer Done")                                      
> 
>     
>                                                     }else{
>                                                       Log.d("Printer", "Printer Error")   
>                                                       //need to break the loop exit to the outer For-Loop
>                                                     }
>                                                 }
>                                             }
>     
>                                             prograssDialog.dismissWithAnimation()
>     
>                                         }
>                                         .show()
>                             
>                         }
>     
>     
>     
>     
>                     }
>     
>                 }
>             }

So according to the code, I want to Pause the process for the user confirmation and if the user says 'Yes' then printer should start print the copy of the document. If the user says 'No' then the loop should break out and continue with the outer For-Loop. When I run this function I'm getting the following message on Logcat:

Skipped 255 frames! The application may be doing too much work on its main thread.

I've even tried While loop blocking and Threading but non of them seems working as expected. What would be the correct way to implement such a use-case? Any kind of guidance would be much appreciated.

Upvotes: 0

Views: 147

Answers (1)

Joozd
Joozd

Reputation: 581

Basically, don't block the UI thread. What you could do, is do the printing stuff in the background (I personally like to use anko's doAsync for that, popping up a dialog on the UI thread (with runOnUiThread) when it is required.

That way you won't be blocking the user interface and the system won't be complaining

Edit: Also I used callback functions to identify whether the printing task has finished or not.

Upvotes: 1

Related Questions