Issue
Everyone, I have this method and want to emit listmessage but list is unable to get the list of values, How can I do it?
fun ReadMessage(dialog: QBChatDialog) = flow{
try {
var list: ArrayList<QBChatMessage> = ArrayList()
chatHelper.ReadChatHistory(dialog, object : QBEntityCallback<ArrayList<QBChatMessage>>{
override fun onSuccess(listmessage: ArrayList<QBChatMessage>?, p1: Bundle?) {
Log.e(TAG, "Reading Message: $p0")
}
override fun onError(p0: QBResponseException?) {
Log.e(TAG, "Reading Message Exception: $p0")
}
})
Log.e(TAG, "Reading Messageeeeeeeeee: $list")
emit(list)
}catch (e: Exception){
Log.e(TAG, "Reading Message Exceptionn: $e")
}
}
Solution
You should use CompletableDeferred
. You can do something like this:
fun readMessage(dialog: QBChatDialog): Flow<ArrayList<QBChatMessage>> {
val historyDeferred = CompletableDeferred<ArrayList<QBChatMessage>>()
chatHelper.ReadChatHistory(dialog, object : QBEntityCallback<ArrayList<QBChatMessage>> {
override fun onSuccess(listmessage: ArrayList<QBChatMessage>?, p1: Bundle?) {
historyDeferred.complete(listmessage ?: arrayListOf())
}
override fun onError(p0: QBResponseException?) {
historyDeferred.completeExceptionally(p0 ?: CancellationException())
}
})
return flow {
try {
emit(historyDeferred.await())
} catch (e: Exception) {
Log.e(TAG, "Reading Message Exceptionn: $e")
}
}
}
Answered By - Juan Fraga
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.