diff --git a/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DatabaseReferenceExt.kt b/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DatabaseReferenceExt.kt index 5b50633..d0f87f2 100644 --- a/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DatabaseReferenceExt.kt +++ b/realtimedb/src/main/kotlin/de/sipgate/federmappe/realtimedb/DatabaseReferenceExt.kt @@ -8,13 +8,9 @@ import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow import kotlinx.serialization.ExperimentalSerializationApi -import kotlinx.serialization.KSerializer -import kotlinx.serialization.SerializationException -import kotlinx.serialization.serializer @ExperimentalSerializationApi inline fun DatabaseReference.toObject( - serializer: KSerializer = serializer(), crossinline errorHandler: (Throwable) -> T? = { throw it } ): Flow = callbackFlow { val valueEventListener = object : ValueEventListener { @@ -23,14 +19,7 @@ inline fun DatabaseReference.toObject( } override fun onDataChange(dataSnapshot: DataSnapshot) { - val decodedAppData = - try { - dataSnapshot.toObjectWithSerializer(serializer) - } catch (ex: SerializationException) { - errorHandler(ex) - } - - trySend(decodedAppData) + trySend(dataSnapshot.toObject(errorHandler = errorHandler)) } } @@ -41,7 +30,6 @@ inline fun DatabaseReference.toObject( @ExperimentalSerializationApi inline fun DatabaseReference.toObjects( - serializer: KSerializer = serializer(), crossinline errorHandler: (Throwable) -> T? = { throw it } ): Flow> = callbackFlow { val valueEventListener = object : ValueEventListener { @@ -50,15 +38,9 @@ inline fun DatabaseReference.toObjects( } override fun onDataChange(dataSnapshot: DataSnapshot) { - val decodedAppData = dataSnapshot.children.mapNotNull { childSnapshot -> - try { - childSnapshot.toObjectWithSerializer(serializer) - } catch (ex: SerializationException) { - errorHandler(ex) - } - } - - trySend(decodedAppData) + trySend(dataSnapshot.children.mapNotNull { childSnapshot -> + childSnapshot.toObject(errorHandler = errorHandler) + }) } }