-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Database transactions #199
base: master
Are you sure you want to change the base?
Changes from 5 commits
63d290a
b5a0b30
0b84470
1536e42
aaca522
8b63096
ab8d5d2
900426b
c6fb31b
50e33d6
fcc97ce
515b7da
872c1a1
98fd19a
1d8b989
b4fbdb1
59bbacf
b9688c9
7350bb3
2f7a650
6ae78af
eccce95
06634d2
b97957d
7425a6b
a2332de
6e41e70
87bb543
9ae03ae
6aef9dc
1ed1028
1546e58
320846c
b928409
3c95915
cbcdd48
2dff075
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,26 +5,27 @@ | |
package dev.gitlive.firebase.database | ||
|
||
import com.google.android.gms.tasks.Task | ||
import com.google.firebase.database.ChildEventListener | ||
import com.google.firebase.database.Logger | ||
import com.google.firebase.database.* | ||
import com.google.firebase.database.ServerValue | ||
import com.google.firebase.database.ValueEventListener | ||
import dev.gitlive.firebase.Firebase | ||
import dev.gitlive.firebase.FirebaseApp | ||
import dev.gitlive.firebase.database.ChildEvent.Type | ||
import dev.gitlive.firebase.decode | ||
import dev.gitlive.firebase.safeOffer | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.FlowPreview | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.produceIn | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.selects.select | ||
import kotlinx.coroutines.tasks.asDeferred | ||
import kotlinx.coroutines.tasks.await | ||
import kotlinx.serialization.DeserializationStrategy | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.SerializationStrategy | ||
|
||
@PublishedApi | ||
|
@@ -188,8 +189,30 @@ actual class DatabaseReference internal constructor( | |
actual suspend fun removeValue() = android.removeValue() | ||
.run { if(persistenceEnabled) await() else awaitWhileOnline() } | ||
.run { Unit } | ||
} | ||
|
||
actual suspend fun <T> runTransaction(strategy: KSerializer<T>, transactionUpdate: (currentData: T) -> T): DataSnapshot { | ||
val deferred = CompletableDeferred<Result<DataSnapshot>>() | ||
android.runTransaction(object : Transaction.Handler { | ||
|
||
override fun doTransaction(currentData: MutableData) = | ||
Transaction.success(transactionUpdate(decode(strategy, currentData)) as MutableData) | ||
|
||
override fun onComplete( | ||
error: DatabaseError?, | ||
committed: Boolean, | ||
snapshot: com.google.firebase.database.DataSnapshot? | ||
) { | ||
if (error == null && snapshot != null) { | ||
deferred.complete(Result.success(DataSnapshot(snapshot))) | ||
} else { | ||
deferred.complete(Result.failure(Throwable(error?.message))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we've got a database specific exception class you can throw here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think you need to use the Result type and use https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-completable-deferred/complete-exceptionally.html instead of throwing |
||
} | ||
} | ||
|
||
}) | ||
return deferred.await().getOrThrow() | ||
} | ||
} | ||
@Suppress("UNCHECKED_CAST") | ||
actual class DataSnapshot internal constructor(val android: com.google.firebase.database.DataSnapshot) { | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm suggesting this change because I think transactions are not working.
We need to work with
currentData.value
, not withcurrentData
itself.Also we need to tolerate
null
value as described in the Firebase documentation.I'm happy to help to update and merge this PR but I'm not sure I'll be able to do the same in js and iOS.