I'm creating an android app that reads and stores the contents of a specific spreadsheet file in realm database. This app is using Sheets API and reactivex. However, when I run it it always ends up in a failure that nothing is stored after reading the file. According to the result of debugging, the Map object that stores contents in the spreadsheet which is updated during the subscription of Observable is always empty. So, I guess it's be because the execution defined in the Observable.fromCallable is never called. However, I cannot figure out how to fix it.
Here are codes of the reading procedure of this app. Could someone please helm me solving this problem?
P.S. In order to implement this program, I quoted the majority of the code from the website below.
https://medium.com/@pedrocarrillo/creating-a-spreadsheet-in-android-210d68412a2e
private fun startReadingSpreadsheet(spreadSheetId : String, range : String) {
dictMap.clear()
readSpreadSheetDisposable =
sheetsRepository.readSpreadSheet(spreadSheetId, range)
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.doOnError { view.showError("Error!") }
.subscribe(Consumer {
val pairs = it
pairs.map { dictMap[it.first.toString()] = dictMap[it.second.toString()]}
Log.d("Sheet :", it.toString())
})
}
class SheetsRepository(private val sheetsAPIDataSource: SheetsAPIDataSource) {
fun readSpreadSheet(spreadsheetId : String,
spreadsheetRange : String): Single<MutableList<Pair<Any, Any>>> {
return sheetsAPIDataSource.readSpreadSheet(spreadsheetId, spreadsheetRange)
}
}
override fun readSpreadSheet(spreadsheetId: String,
spreadsheetRange: String): Single<MutableList<Pair<Any, Any>>> {
return Observable
.fromCallable {
Log.d("spread sheet reading", "started!")
val response = sheetsAPI.spreadsheets()
.values()
.get(spreadsheetId, spreadsheetRange)
.execute()
response.getValues()
}.map { it[0].zip(it[1]) }.flatMapIterable { it -> it }
.toList()
}