Skip to content

Commit

Permalink
feature: Coroutine Task Wrapper (#1064)
Browse files Browse the repository at this point in the history
* feature: Coroutine Task Wrapper
- Add extension functions to wrap Task in coroutine
- Update libs

* feature: Coroutine Task Wrapper
- Suppress unused
- Correct logic for background threading
  • Loading branch information
phillwiggins authored Sep 30, 2020
1 parent 797786f commit 2877ca0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
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)
}
}

0 comments on commit 2877ca0

Please sign in to comment.