Reputation: 23
I am not sure what I am doing wrong here but my main thread is blocked (as per Android studio's message : I/Choreographer: Skipped 1009 frames! The application may be doing too much work on its main thread.) although I am using a seperate thread to create my PDF using itext.
+
My spinner class, my toast do not show as well, both are written in main thread (perhaps because of main thread being unresponsive as per the message). Once the PDF is created, the dialog shows up as normal and everything starts working as expected.
val bmap = BitmapFactory.decodeResource([email protected],R.drawable.camera_item_center)
val threadPDF = Thread(object : Runnable {
override fun run() {
CreatePDF(file, site, bmap).makePDF()
}
})
threadPDF.start()
spinner.setVisibility(View.VISIBLE)
Toast.makeText(this@SiteItemsActivity,"Please wait....",Toast.LENGTH_SHORT).show()
while (threadPDF.isAlive){
//Thread.sleep(1)
}
spinner.setVisibility(View.GONE);
showDialog()
Upvotes: 0
Views: 324
Reputation: 1007276
although I am using a seperate thread to create my PDF using itext
But then you are tying up the main application thread with an continuous loop (while (threadPDF.isAlive)
).
Delete that while
loop. Use other solutions for updating your UI when the work is done. For example, you could:
replace your thread with an AsyncTask
and update your UI in onPostExecute()
have your thread post()
a Runnable
(using your Spinner
) to have that Runnable
be run on the main application thread after your thread's work is done
replace your thread with an RxJava
Observable
, where you schedule its work a background thread (e.g., Schedulers.io()
) and observe it on the main application thread (AndroidSchedulers.mainThread()
)
wrap your thread in a LiveData
object and have your UI code observe that LiveData
for the completion of the work
and so on
Upvotes: 1