Skip to content

Commit f085fe7

Browse files
oliveiradevJawnnypoo
authored andcommitted
Enhancement/parsequery operations (#972)
* Add more operation extensions to parse query * Add a coroutine builder to call parse query operations as a receiver * Change package structure * Add parse object support
1 parent 39e0da3 commit f085fe7

10 files changed

+210
-38
lines changed

coroutines/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ launch { // Coroutine builder
2626
```
2727
We use a coroutine builder because `find()` is a suspend function.
2828

29+
We can also, use a function like a coroutine builder, it will be provider us a flexibility call our query without any extensions function.
30+
31+
````kotlin
32+
launchQuery(query) {
33+
// doing operations like find, get, first and count
34+
}
35+
````
36+
37+
It uses a a regular coroutine builder `launch` and pass as receiver a `ParseQueryOperation``
38+
2939
### ParseCloud
3040

3141
We can call cloud function inline:
@@ -60,6 +70,10 @@ launch { // Coroutine builder
6070
}
6171
```
6272

73+
### Parse Object
74+
75+
We can save, pinning and fetch parse objects use coroutines as well.
76+
6377
## Contributing
6478
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.
6579

coroutines/src/main/java/com/parse/coroutines/ParseQueryCoroutinesExtensions.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

coroutines/src/main/java/com/parse/coroutines/ParseCloudCoroutinesExtensions.kt renamed to coroutines/src/main/java/com/parse/coroutines/cloudfunction/ParseCloudCoroutinesExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@file:JvmName("ParseCloudCoroutinesExtensions")
22

3-
package com.parse.coroutines
3+
package com.parse.coroutines.cloudfunction
44

55
import com.parse.ParseCloud
66
import kotlin.coroutines.resume
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.parse.coroutines.read
2+
3+
interface ParseQueryOperation<out T> {
4+
suspend fun find(): List<T>
5+
suspend fun get(id: String): T
6+
suspend fun first(): T
7+
suspend fun count(): Int
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.parse.coroutines.read
2+
3+
import com.parse.ParseObject
4+
import com.parse.ParseQuery
5+
import com.parse.coroutines.read.query.countInternal
6+
import com.parse.coroutines.read.query.findInternal
7+
import com.parse.coroutines.read.query.firstInternal
8+
import com.parse.coroutines.read.query.getInternal
9+
10+
class ParseQueryOperationImpl<T : ParseObject>(private val query: ParseQuery<T>) : ParseQueryOperation<T> {
11+
12+
override suspend fun find(): List<T> = query.findInternal()
13+
14+
override suspend fun get(id: String): T = query.getInternal(id)
15+
16+
override suspend fun first(): T = query.firstInternal()
17+
18+
override suspend fun count(): Int = query.countInternal()
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@file:JvmName("ParseObjectCoroutinesExtensions")
2+
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")
3+
4+
package com.parse.coroutines.read.parse_object
5+
6+
import com.parse.ParseObject
7+
import kotlin.coroutines.resume
8+
import kotlin.coroutines.resumeWithException
9+
import kotlin.coroutines.suspendCoroutine
10+
11+
suspend fun <T : ParseObject> ParseObject.fetch(): T {
12+
return suspendCoroutine { continuation ->
13+
14+
fetchInBackground<T> { obj, e ->
15+
if (e == null) continuation.resume(obj)
16+
else continuation.resumeWithException(e)
17+
}
18+
}
19+
}
20+
21+
suspend fun <T : ParseObject> ParseObject.fetchIfNeeded(): T {
22+
return suspendCoroutine { continuation ->
23+
24+
fetchIfNeededInBackground<T> { obj, e ->
25+
if (e == null) continuation.resume(obj)
26+
else continuation.resumeWithException(e)
27+
}
28+
}
29+
}
30+
31+
suspend fun <T : ParseObject> ParseObject.fetchFromLocal(): T {
32+
return suspendCoroutine { continuation ->
33+
34+
fetchFromLocalDatastoreInBackground<T> { obj, e ->
35+
if (e == null) continuation.resume(obj)
36+
else continuation.resumeWithException(e)
37+
}
38+
}
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@file:JvmName("ParseQueryCoroutinesBuilder")
2+
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")
3+
4+
package com.parse.coroutines.read.query
5+
6+
import com.parse.ParseObject
7+
import com.parse.ParseQuery
8+
import com.parse.coroutines.read.ParseQueryOperation
9+
import com.parse.coroutines.read.ParseQueryOperationImpl
10+
import kotlinx.coroutines.CoroutineScope
11+
import kotlinx.coroutines.Job
12+
import kotlinx.coroutines.launch
13+
import kotlin.coroutines.CoroutineContext
14+
import kotlin.coroutines.EmptyCoroutineContext
15+
16+
fun <T : ParseObject> CoroutineScope.launchQuery(
17+
query: ParseQuery<T>,
18+
context: CoroutineContext = EmptyCoroutineContext,
19+
block: suspend ParseQueryOperation<T>.() -> Unit
20+
) : Job {
21+
return launch(context) {
22+
block.invoke(ParseQueryOperationImpl(query))
23+
}
24+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
@file:JvmName("ParseQueryCoroutinesExtensions")
2+
3+
package com.parse.coroutines.read.query
4+
5+
import com.parse.ParseObject
6+
import com.parse.ParseQuery
7+
import kotlinx.coroutines.suspendCancellableCoroutine
8+
import kotlin.coroutines.resume
9+
import kotlin.coroutines.resumeWithException
10+
11+
suspend fun <T : ParseObject> ParseQuery<T>.find(): List<T> {
12+
return findInternal()
13+
}
14+
15+
internal suspend fun <T : ParseObject> ParseQuery<T>.findInternal(): List<T> {
16+
return suspendCancellableCoroutine { continuation ->
17+
continuation.invokeOnCancellation {
18+
cancel()
19+
}
20+
21+
findInBackground { objects, e ->
22+
if (e == null) continuation.resume(objects)
23+
else continuation.resumeWithException(e)
24+
}
25+
}
26+
}
27+
28+
suspend fun <T : ParseObject> ParseQuery<T>.get(id: String): T {
29+
return getInternal(id)
30+
}
31+
32+
internal suspend fun <T : ParseObject> ParseQuery<T>.getInternal(id: String): T {
33+
return suspendCancellableCoroutine { continuation ->
34+
continuation.invokeOnCancellation {
35+
cancel()
36+
}
37+
38+
getInBackground(id) { obj, e ->
39+
if (e == null) continuation.resume(obj)
40+
else continuation.resumeWithException(e)
41+
}
42+
}
43+
}
44+
45+
suspend fun <T : ParseObject> ParseQuery<T>.first(): T {
46+
return firstInternal()
47+
}
48+
49+
internal suspend fun <T : ParseObject> ParseQuery<T>.firstInternal(): T {
50+
return suspendCancellableCoroutine { continuation ->
51+
continuation.invokeOnCancellation {
52+
cancel()
53+
}
54+
55+
getFirstInBackground { obj, e ->
56+
if (e == null) continuation.resume(obj)
57+
else continuation.resumeWithException(e)
58+
}
59+
}
60+
}
61+
62+
suspend fun <T : ParseObject> ParseQuery<T>.count(): Int {
63+
return countInternal()
64+
}
65+
66+
internal suspend fun <T : ParseObject> ParseQuery<T>.countInternal(): Int {
67+
return suspendCancellableCoroutine { continuation ->
68+
continuation.invokeOnCancellation {
69+
cancel()
70+
}
71+
72+
countInBackground { count, e ->
73+
if (e == null) continuation.resume(count)
74+
else continuation.resumeWithException(e)
75+
}
76+
}
77+
}

coroutines/src/main/java/com/parse/coroutines/ParseUserCoroutinesExtensions.kt renamed to coroutines/src/main/java/com/parse/coroutines/user/ParseUserCoroutinesExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@file:JvmName("ParseUserCoroutinesExtensions")
22

3-
package com.parse.coroutines
3+
package com.parse.coroutines.user
44

55
import com.parse.ParseUser
66
import kotlin.coroutines.resume
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@file:JvmName("ParseObjectCoroutinesWriteExtensions")
2+
@file:Suppress("EXTENSION_SHADOWED_BY_MEMBER")
3+
4+
package com.parse.coroutines.write.parse_object
5+
6+
import com.parse.ParseObject
7+
import kotlin.coroutines.resume
8+
import kotlin.coroutines.resumeWithException
9+
import kotlin.coroutines.suspendCoroutine
10+
11+
suspend fun ParseObject.save() {
12+
return suspendCoroutine { continuation ->
13+
saveInBackground {
14+
if (it == null) continuation.resume(Unit)
15+
else continuation.resumeWithException(it)
16+
}
17+
}
18+
}
19+
20+
suspend fun ParseObject.pin() {
21+
return suspendCoroutine { continuation ->
22+
pinInBackground {
23+
if (it == null) continuation.resume(Unit)
24+
else continuation.resumeWithException(it)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)