Daniel
12/13/2020, 12:20 PMreified
in this mehod?
I want to have a method which loads a value from the firebase database and the value needs to be properly typed when returned
class OnlinePersistence {
suspend inline fun <reified T> loadValue(path: OnlinePersistencePath, default: T) =
withContext(IO) {
suspendCancellableCoroutine<T> { continuation ->
FirebaseDatabase
.getInstance()
.getReference(path.rawPath)
.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if(continuation.isActive) {
val value = snapshot.getValue<T>()
if(value != null) {
continuation.resume(value)
} else {
continuation.resume(default)
}
}
}
override fun onCancelled(error: DatabaseError) {
if(continuation.isActive) {
continuation.resume(default)
}
}
})
}
}
}
All well and good now, but when I extract
FirebaseDatabase.getInstance()
into the constructor I cannot make it private like it should be. (Since the method is inline because of reified)Robert Jaros
12/13/2020, 12:55 PMClass
. Then you will be able to pass this parameter to the getValue
method: https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot#getValue(java.lang.Class%3CT%3E)Daniel
12/13/2020, 1:04 PMList<MyClass>
, then the clazz approach is at its limit, correct? since List<MyClass>::class.java
is no possibleRobert Jaros
12/13/2020, 1:08 PM