Hi kotliers and a huge thanks in advance for readi...
# android
d
Hi kotliers and a huge thanks in advance for reading my message! Question to following case: • Is it possible to eliminate
reified
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
Copy code
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
Copy code
FirebaseDatabase.getInstance()
into the constructor I cannot make it private like it should be. (Since the method is inline because of reified)
r
Instead of making an inline function with reified type parameter, you can make normal function but add third parameter of type
Class
. 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)
d
Thank you, currently trying this out. When T is
List<MyClass>
, then the clazz approach is at its limit, correct? since
List<MyClass>::class.java
is no possible