我有对象数组, 我想为这个数组的每个对象创建一个pdf。要做到这一点,我使用循环,但问题是它创建了最后一个的PDF文件。
我想我知道为什么,因为任务没有时间完成它继续下一个。
我想在创建pdf文件之前停止循环,然后恢复下一次迭代......
每次迭代我想停止循环以给出创建pdf文件的时间
这是我的代码:
for (i in 0 until multipleInvoice!!.size) {
receiveUser = MainActivity.users.find { it.codeZ == multipleInvoice!![i].customerCodeZ }
products = multipleInvoice!![i].retrieveInvoice(this)
receiveInvoice = multipleInvoice!![i]
doAsync {
// Long background task
progressBar.setVisible()
htmlContent = renderHTML()
uiThread {
//webViewInitializer(htmlContent)
doAsync {
htmlToPDF(htmlContent) //Is function create PDF file on the phone!!!
//Here I want to stop the loop until the file is created
// (by the function of the line above). As soon as the file is created, we take the loop
}
progressBar.setGone()
}
}
}
答案 0 :(得分:1)
你不能使用任何循环来实现你想要的。有一种方法可以使用递归执行您想要的操作。当您有multipleInvoice时,将此函数调用为第0个位置以呈现PDF。
if(multipleInvoice?:0 >0)
renderInvoice(0)
fun renderInvoice(i:Int) {
receiveUser = MainActivity.users.find { it.codeZ == multipleInvoice!![i].customerCodeZ }
products = multipleInvoice!![i].retrieveInvoice(this)
receiveInvoice = multipleInvoice!![i]
doAsync {
// Long background task
progressBar.setVisible()
htmlContent = renderHTML()
uiThread {
//webViewInitializer(htmlContent)
doAsync {
htmlToPDF(htmlContent) //Is function create PDF file on the phone!!!
//Here I want to stop the loop until the file is created
// (by the function of the line above). As soon as the file is created, we take the loop
if(i< multipleInvoice!!.size)
renderInvoice(++i)
}
progressBar.setGone()
}
}
}