/** Start processing the images for text recognition */ fun startProcessing(onFinish: (List) -> Unit) { val result = mutableListOf() Log.d(TAG, "Images to process: ${taskList.size}") if (taskList.isEmpty()) { return } taskList.forEach { image -> Log.i(TAG, "Start") val startTime = Date().time detector.processImage(image) // runs asynchronous .addOnSuccessListener { firebaseVisionText -> val elapsedMilliseconds = Date().time - startTime Log.i(TAG, "Text recognition took ${elapsedMilliseconds / 1000} seconds") Log.d(TAG, "Processing is running in [${Thread.currentThread().name}] thread") Log.i(TAG, "list size: ${taskList.size}") Log.e(TAG, firebaseVisionText.text) result.add(firebaseVisionText.text) } .addOnFailureListener { e -> Log.e(TAG, "Processing image failed. ${e.cause}") }.addOnCompleteListener { Log.i(TAG, "Completed!") } } Log.i(TAG, "After for loop") // how to wait for processing multiple images in taskList onFinish(result) }