nglauber
03/05/2021, 2:40 PMYoussef Shoaib [MOD]
03/05/2021, 2:54 PMnglauber
03/05/2021, 3:05 PMclass CitySfRepository : BaseSfRepository() {
override suspend fun cityById(id: Int): City? =
getObject( "SELECT Id, ID__c, Name FROM $objectType WHERE ID__c = $id")
}
This getObject
function is defined in the parent class BaseSfRepository
protected suspend inline fun <reified T> getObject(q: String): T? =
suspendCoroutine { continuation ->
sendAsync<List<T>?>(
RestRequest.getRequestForQuery(
ApiVersionStrings.VERSION_NUMBER,
q
),
{ list ->
continuation.resume(list?.first())
},
{ exception ->
continuation.resumeWithException(exception)
}
)
}
In this sendAsync
function I’m getting the response and parsing using Gson library
protected inline fun <reified T> parseResponse(
restResponse: RestResponse
): T? {
val x = gson.fromJson<T?>(
restResponse.asJSONObject().getJSONArray("records").toString(),
object : TypeToken<T?>() {}.type
)
return x
}
I’m getting this error:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.test.mycompany.City
Youssef Shoaib [MOD]
03/05/2021, 3:14 PMnglauber
03/05/2021, 3:16 PMsendAsync<List<T>?>
. Because the Gson is returning a list of List<LinkedTreeMap>
when it parses the JSONnglauber
03/05/2021, 3:19 PM// From CitySfRepository
override suspend fun allCities(): List<City> =
getObjectList("SELECT Id, ID__c, Name FROM $objectType ORDER BY Name") ?: emptyList()
nglauber
03/05/2021, 3:20 PMgetObjectList
passes the T
directly…
protected suspend inline fun <reified T> getObjectList(q: String): T? =
suspendCoroutine { continuation ->
sendAsync<T?>(
RestRequest.getRequestForQuery(
ApiVersionStrings.VERSION_NUMBER,
q
),
{ list ->
continuation.resume(list)
},
{ exception ->
continuation.resumeWithException(exception)
}
)
}
nglauber
03/05/2021, 3:31 PMreified
it 😉
protected suspend inline fun <reified T, reified J: List<T>> getObject(q: String): T? =
suspendCoroutine { continuation ->
sendAsync<J?>(
RestRequest.getRequestForQuery(
ApiVersionStrings.VERSION_NUMBER,
q
),
{ list ->
continuation.resume(list?.first())
},
{ exception ->
continuation.resumeWithException(exception)
}
)
}
Youssef Shoaib [MOD]
03/05/2021, 3:41 PM