Skip to content
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

feature: Coroutine Task Wrapper #1064

Merged
merged 2 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = "1.3.72"
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
Expand Down
27 changes: 27 additions & 0 deletions coroutines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,33 @@ launch { // Coroutine builder

We can save, pinning and fetch parse objects use coroutines as well.

## Task Wrapper
Coroutine support can be provided as an extension method on any `Task`. For example:
```kotlin
suspend fun anonymousLogin() {
try {
val user = ParseAnonymousUtils.logInInBackground().suspendGet()
Timber.d("Logged in with user ${user.objectId}")
} catch (e: ParseException) {
Timber.e(e)
}
}
```
Tasks with a Void results, ie. `Task<Void>` can be made into a `Completable`.
For example:
```kotlin
suspend fun updateUserLastLogIn() {
val user = ParseUser.getCurrentUser()
user.put("lastLoggedIn", System.currentTimeMillis())
try {
user.saveInBackground().suspendRun()
Timber.d("user saved")
} catch (e: ParseException) {
Timber.e(it)
}
}
```

## Contributing
When contributing to the `coroutines` module, please first consider if the extension function you are wanting to add would potentially be better suited in the main `parse` module. If it is something specific to Kotlin users or only useful in a Kotlin project, feel free to make a PR adding it to this module. Otherwise, consider adding the addition to the `parse` module itself, so that it is still usable in Java.

Expand Down
2 changes: 1 addition & 1 deletion coroutines/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ android {
}

ext {
coroutinesVersion = "1.3.3"
coroutinesVersion = "1.3.9"
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@file:JvmName("ParseTaskExtensions")
@file:Suppress("unused")

package com.parse.coroutines

import com.parse.boltsinternal.Task
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

@Suppress("BlockingMethodInNonBlockingContext")
suspend fun <T> Task<T>.suspendGet(dispatcher: CoroutineDispatcher = Dispatchers.IO) = withContext<T>(dispatcher) {
return@withContext suspendCoroutine { continuation ->
waitForCompletion()
if (isFaulted) {
continuation.resumeWithException(error)
}
continuation.resume(result)
}
}

@Suppress("BlockingMethodInNonBlockingContext")
suspend fun Task<Void>.suspendRun(dispatcher: CoroutineDispatcher = Dispatchers.IO) = withContext<Unit>(dispatcher) {
return@withContext suspendCoroutine { continuation ->
waitForCompletion()
if (isFaulted) {
continuation.resumeWithException(error)
}
continuation.resume(Unit)
}
}