Skip to content
This repository was archived by the owner on Oct 27, 2019. It is now read-only.

Commit 95aa9aa

Browse files
committed
Change DatabaseReference coroutine implementation
Change from suspendCoroutine to suspendCancellableCoroutine
1 parent a297e65 commit 95aa9aa

File tree

1 file changed

+21
-8
lines changed
  • kotlinx-coroutines-firebase-android/src/main/java/kotlinx/coroutines/firebase/android

1 file changed

+21
-8
lines changed

kotlinx-coroutines-firebase-android/src/main/java/kotlinx/coroutines/firebase/android/DatabaseReference.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import com.google.firebase.database.DataSnapshot
33
import com.google.firebase.database.DatabaseError
44
import com.google.firebase.database.DatabaseReference
55
import com.google.firebase.database.ValueEventListener
6+
import kotlinx.coroutines.experimental.cancel
7+
import kotlinx.coroutines.experimental.disposeOnCompletion
8+
import kotlinx.coroutines.experimental.suspendCancellableCoroutine
69
import kotlin.coroutines.experimental.suspendCoroutine
710

811
/**
@@ -12,7 +15,8 @@ import kotlin.coroutines.experimental.suspendCoroutine
1215
* main coroutine code that reads a single value from Firebase Database. To use it's functionality
1316
* call [readValue] function.
1417
*
15-
* The implementation consists of a [suspendCoroutine] that encapsulates a [ValueEventListener].
18+
* The implementation consists of a [suspendCancellableCoroutine] that encapsulates a
19+
* [ValueEventListener].
1620
*
1721
* @param reference The Firebase Database node to be read.
1822
* @param type Expected type wrapped in a Class instance.
@@ -22,8 +26,8 @@ import kotlin.coroutines.experimental.suspendCoroutine
2226
suspend fun <T : Any> readReference(
2327
reference: DatabaseReference,
2428
type: Class<T>
25-
): T = suspendCoroutine { continuation ->
26-
reference.addListenerForSingleValueEvent(object : ValueEventListener {
29+
): T = suspendCancellableCoroutine { continuation ->
30+
val listener = object : ValueEventListener {
2731

2832
/**
2933
* Callback to handle Firebase Database errors
@@ -62,7 +66,11 @@ suspend fun <T : Any> readReference(
6266
continuation.resumeWithException(exception)
6367
}
6468
}
65-
})
69+
}
70+
71+
continuation.invokeOnCompletion { reference.removeEventListener(listener) }
72+
73+
reference.addListenerForSingleValueEvent(listener)
6674
}
6775

6876
/**
@@ -109,7 +117,8 @@ suspend inline fun <reified T : Any> DatabaseReference.readValue(): T = readValu
109117
* main coroutine code that reads a collection of values from Firebase Database. To use it's
110118
* functionality call [readValue] function.
111119
*
112-
* The implementation consists of a [suspendCoroutine] that encapsulates a [ValueEventListener].
120+
* The implementation consists of a [suspendCancellableCoroutine] that encapsulates a
121+
* [ValueEventListener].
113122
*
114123
* @param reference The Firebase Database node to be read.
115124
* @param type Expected type wrapped in a Class instance.
@@ -119,8 +128,8 @@ suspend inline fun <reified T : Any> DatabaseReference.readValue(): T = readValu
119128
suspend fun <T : Any> readReferences(
120129
reference: DatabaseReference,
121130
type: Class<T>
122-
): Collection<T> = suspendCoroutine { continuation ->
123-
reference.addListenerForSingleValueEvent(object : ValueEventListener {
131+
): Collection<T> = suspendCancellableCoroutine { continuation ->
132+
val listener = object : ValueEventListener {
124133

125134
/**
126135
* Callback to handle Firebase Database errors
@@ -159,7 +168,11 @@ suspend fun <T : Any> readReferences(
159168
continuation.resumeWithException(exception)
160169
}
161170
}
162-
})
171+
}
172+
173+
continuation.invokeOnCompletion { reference.removeEventListener(listener) }
174+
175+
reference.addListenerForSingleValueEvent(listener)
163176
}
164177

165178
/**

0 commit comments

Comments
 (0)