-
-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Coroutine Task Wrapper (#1064)
* 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
1 parent
797786f
commit 2877ca0
Showing
4 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ android { | |
} | ||
|
||
ext { | ||
coroutinesVersion = "1.3.3" | ||
coroutinesVersion = "1.3.9" | ||
} | ||
|
||
dependencies { | ||
|
35 changes: 35 additions & 0 deletions
35
coroutines/src/main/java/com/parse/coroutines/ParseTaskExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|