I'm new to kotlin. I have encountered a problem where the CountDownLatch is not unlocking, I am not sure whether which line causing the problem for the infinite lock.
class RestaurantOverviewDataSource {
fun loadRestaurants(): List<RestaurantOverview>{
val db = Firebase.firestore
var restaurants : List<RestaurantOverview> = emptyList()
val lock = CountDownLatch(1)
try {
Log.w(TAG, "FIREBASE")
db.collection("tb_mydata").get().addOnSuccessListener { result ->
try{
var count = 0;
for (document in result) {
count++;
var category = document.data["Category"] as String
var name = document.data["Name"] as String
var contact = document.data["Contact"] as String
var addr = document.data["Address"] as String
restaurants += RestaurantOverview(count, R.color.grey1, name, category, 20, 4.1f, 150, true, true, true, true)
Log.d(TAG, "${document.id} => ${document.data}")
}
} finally {
lock.countDown()
}
}.addOnFailureListener { exception ->
try{
Log.w(TAG, "Error getting documents.", exception)
restaurants = listOf(RestaurantOverview(1, R.color.grey1, "KFC 1", "Western / Fast Food", 20, 4.0f, 190, isHalal = true, isVegan = true, isVegetarian = true, isOpen = true))
} finally {
lock.countDown()
}
}.addOnCompleteListener {
Log.w(TAG, "on complete")
lock.countDown()
}
}catch (ignored: NullPointerException) { lock.countDown() }
Log.w(TAG, "INIT AWAIT")
lock.await()
Log.w(TAG, "DONE AWAIT")
return restaurants
}
I have reference and used the suggestion provided at here CountDownLatch not freeing thread, where try{}finally{} is added, but still it's not freeing the thread. Any advice? Thanks in advance.