Skip to content

Commit 8f08848

Browse files
Merge branch '43-rxjava3' into 'dev'
Resolve "Add new artifact supporting RxJava 3" See merge request objectbox/objectbox-java!56
2 parents 0345d22 + 59f7b08 commit 8f08848

File tree

15 files changed

+672
-18
lines changed

15 files changed

+672
-18
lines changed

Jenkinsfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pipeline {
5858
"--tests io.objectbox.FunctionalTestSuite " +
5959
"--tests io.objectbox.test.proguard.ObfuscatedEntityTest " +
6060
"--tests io.objectbox.rx.QueryObserverTest " +
61+
"--tests io.objectbox.rx3.QueryObserverTest " +
6162
"spotbugsMain assemble"
6263
}
6364
}

build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ buildscript {
2020
ob_native_dep = "io.objectbox:objectbox-$objectboxPlatform:$nativeVersion"
2121

2222
junit_version = '4.13'
23+
mockito_version = '3.3.3'
24+
kotlin_version = '1.3.61'
25+
dokka_version = '0.10.1'
2326

2427
println "version=$ob_version"
2528
println "objectboxNativeDependency=$ob_native_dep"
@@ -34,6 +37,8 @@ buildscript {
3437
}
3538

3639
dependencies {
40+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
41+
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
3742
classpath "gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.0.5"
3843
}
3944
}
@@ -64,7 +69,8 @@ def projectNamesToPublish = [
6469
'objectbox-java-api',
6570
'objectbox-java',
6671
'objectbox-kotlin',
67-
'objectbox-rxjava'
72+
'objectbox-rxjava',
73+
'objectbox-rxjava3'
6874
]
6975

7076
def hasSigningProperties() {

objectbox-kotlin/build.gradle

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
buildscript {
22
ext.javadocDir = "$buildDir/docs/javadoc"
3-
ext.kotlin_version = '1.3.61'
4-
ext.dokka_version = '0.10.1'
5-
6-
repositories {
7-
mavenCentral()
8-
jcenter()
9-
}
10-
11-
dependencies {
12-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13-
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
14-
}
153
}
164

175
apply plugin: 'kotlin'
@@ -63,6 +51,7 @@ dependencies {
6351
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
6452

6553
compile project(':objectbox-java')
54+
compileOnly project(':objectbox-rxjava3')
6655
}
6756

6857

objectbox-rxjava/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
:information_source: This library will receive no new features.
2+
Development will continue with the [RxJava 3 APIs for ObjectBox](/objectbox-rxjava3).
3+
14
RxJava 2 APIs for ObjectBox
25
===========================
36
While ObjectBox has [data observers and reactive extensions](https://docs.objectbox.io/data-observers-and-rx) built-in,
@@ -13,7 +16,7 @@ For general object changes, you can use `RxBoxStore` to create an `Observable`.
1316
For example to get query results and subscribe to future updates (Object changes will automatically emmit new data):
1417

1518
```java
16-
Query query = box.query().build();
19+
Query<User> query = box.query().build();
1720
RxQuery.observable(query).subscribe(this);
1821
```
1922

@@ -27,5 +30,3 @@ implementation "io.objectbox:objectbox-rxjava:$objectboxVersion"
2730
Links
2831
-----
2932
[Data Observers and Rx Documentation](https://docs.objectbox.io/data-observers-and-rx)
30-
31-
[Note App example](https://github.com/objectbox/objectbox-examples/blob/master/objectbox-example/src/main/java/io/objectbox/example/ReactiveNoteActivity.java)

objectbox-rxjava/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ dependencies {
88
compile 'io.reactivex.rxjava2:rxjava:2.2.18'
99

1010
testCompile "junit:junit:$junit_version"
11-
// Mockito 3.x requires Java 8.
12-
testCompile 'org.mockito:mockito-core:2.28.2'
11+
testCompile "org.mockito:mockito-core:$mockito_version"
1312
}
1413

1514
task javadocJar(type: Jar, dependsOn: javadoc) {

objectbox-rxjava3/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
RxJava 3 APIs for ObjectBox
2+
===========================
3+
While ObjectBox has [data observers and reactive extensions](https://docs.objectbox.io/data-observers-and-rx) built-in,
4+
this project adds RxJava 3 support.
5+
6+
For general object changes, you can use `RxBoxStore` to create an `Observable`.
7+
8+
`RxQuery` allows you to interact with ObjectBox `Query` objects using:
9+
* Flowable
10+
* Observable
11+
* Single
12+
13+
For example to get query results and subscribe to future updates (Object changes will automatically emmit new data):
14+
15+
```java
16+
Query<User> query = box.query().build();
17+
RxQuery.observable(query).subscribe(this);
18+
```
19+
20+
Adding the library to your project
21+
-----------------
22+
Grab via Gradle:
23+
```gradle
24+
implementation "io.objectbox:objectbox-rxjava3:$objectboxVersion"
25+
```
26+
27+
Migrating from RxJava 2
28+
-----------------------
29+
30+
If you have previously used the ObjectBox RxJava library note the following changes:
31+
32+
- The location of the dependency has changed to `objectbox-rxjava3` (see above).
33+
- The package name has changed to `io.objectbox.rx3` (from `io.objectbox.rx`).
34+
35+
This should allow using both versions side-by-side while you migrate your code to RxJava 3.
36+
37+
Links
38+
-----
39+
[Data Observers and Rx Documentation](https://docs.objectbox.io/data-observers-and-rx)

objectbox-rxjava3/build.gradle

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
buildscript {
2+
ext.javadocDir = "$buildDir/docs/javadoc"
3+
}
4+
5+
apply plugin: 'java'
6+
apply plugin: 'kotlin'
7+
apply plugin: 'org.jetbrains.dokka'
8+
9+
sourceCompatibility = JavaVersion.VERSION_1_8
10+
targetCompatibility = JavaVersion.VERSION_1_8
11+
12+
// Produce Java 8 byte code, would default to Java 6.
13+
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
14+
kotlinOptions {
15+
jvmTarget = "1.8"
16+
}
17+
}
18+
19+
dokka {
20+
outputFormat = 'html'
21+
outputDirectory = javadocDir
22+
23+
// Fix "Can't find node by signature": have to manually point to dependencies.
24+
// https://github.com/Kotlin/dokka/wiki/faq#dokka-complains-about-cant-find-node-by-signature-
25+
configuration{
26+
externalDocumentationLink {
27+
// Point to web javadoc for objectbox-java packages.
28+
url = new URL("https://objectbox.io/docfiles/java/current/")
29+
// Note: Using JDK 9+ package-list is now called element-list.
30+
packageListUrl = new URL(url, "element-list")
31+
}
32+
}
33+
}
34+
35+
dependencies {
36+
compile project(':objectbox-java')
37+
compile 'io.reactivex.rxjava3:rxjava:3.0.1'
38+
compileOnly "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
39+
40+
testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
41+
testCompile "junit:junit:$junit_version"
42+
testCompile "org.mockito:mockito-core:$mockito_version"
43+
}
44+
45+
task javadocJar(type: Jar, dependsOn: dokka) {
46+
classifier = 'javadoc'
47+
from "$javadocDir"
48+
}
49+
50+
task sourcesJar(type: Jar) {
51+
from sourceSets.main.allSource
52+
classifier = 'sources'
53+
}
54+
55+
artifacts {
56+
// java plugin adds jar.
57+
archives javadocJar
58+
archives sourcesJar
59+
}
60+
61+
uploadArchives {
62+
repositories {
63+
mavenDeployer {
64+
// Basic definitions are defined in root project
65+
pom.project {
66+
name 'ObjectBox RxJava 3 API'
67+
description 'RxJava 3 extensions for ObjectBox'
68+
69+
licenses {
70+
license {
71+
name 'The Apache Software License, Version 2.0'
72+
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
73+
distribution 'repo'
74+
}
75+
}
76+
}
77+
}
78+
}
79+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.objectbox.rx3
2+
3+
import io.objectbox.query.Query
4+
import io.reactivex.rxjava3.core.BackpressureStrategy
5+
import io.reactivex.rxjava3.core.Flowable
6+
import io.reactivex.rxjava3.core.Observable
7+
import io.reactivex.rxjava3.core.Single
8+
9+
/**
10+
* Shortcut for [`RxQuery.flowableOneByOne(query, strategy)`][RxQuery.flowableOneByOne].
11+
*/
12+
fun <T> Query<T>.flowableOneByOne(strategy: BackpressureStrategy = BackpressureStrategy.BUFFER): Flowable<T> {
13+
return RxQuery.flowableOneByOne(this, strategy)
14+
}
15+
16+
/**
17+
* Shortcut for [`RxQuery.observable(query)`][RxQuery.observable].
18+
*/
19+
fun <T> Query<T>.observable(): Observable<MutableList<T>> {
20+
return RxQuery.observable(this)
21+
}
22+
23+
/**
24+
* Shortcut for [`RxQuery.single(query)`][RxQuery.single].
25+
*/
26+
fun <T> Query<T>.single(): Single<MutableList<T>> {
27+
return RxQuery.single(this)
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2017 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.objectbox.rx3;
18+
19+
import io.objectbox.BoxStore;
20+
import io.objectbox.reactive.DataSubscription;
21+
import io.reactivex.rxjava3.core.Observable;
22+
23+
/**
24+
* Static methods to Rx-ify ObjectBox queries.
25+
*/
26+
public abstract class RxBoxStore {
27+
/**
28+
* Using the returned Observable, you can be notified about data changes.
29+
* Once a transaction is committed, you will get info on classes with changed Objects.
30+
*/
31+
@SuppressWarnings("rawtypes") // BoxStore observer may return any (entity) type.
32+
public static Observable<Class> observable(BoxStore boxStore) {
33+
return Observable.create(emitter -> {
34+
final DataSubscription dataSubscription = boxStore.subscribe().observer(data -> {
35+
if (!emitter.isDisposed()) {
36+
emitter.onNext(data);
37+
}
38+
});
39+
emitter.setCancellable(dataSubscription::cancel);
40+
});
41+
}
42+
43+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2017 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.objectbox.rx3;
18+
19+
import java.util.List;
20+
21+
import io.objectbox.query.Query;
22+
import io.objectbox.reactive.DataSubscription;
23+
import io.reactivex.rxjava3.core.BackpressureStrategy;
24+
import io.reactivex.rxjava3.core.Flowable;
25+
import io.reactivex.rxjava3.core.FlowableEmitter;
26+
import io.reactivex.rxjava3.core.Observable;
27+
import io.reactivex.rxjava3.core.Single;
28+
29+
/**
30+
* Static methods to Rx-ify ObjectBox queries.
31+
*/
32+
public abstract class RxQuery {
33+
/**
34+
* The returned Flowable emits Query results one by one. Once all results have been processed, onComplete is called.
35+
* Uses BackpressureStrategy.BUFFER.
36+
*/
37+
public static <T> Flowable<T> flowableOneByOne(final Query<T> query) {
38+
return flowableOneByOne(query, BackpressureStrategy.BUFFER);
39+
}
40+
41+
/**
42+
* The returned Flowable emits Query results one by one. Once all results have been processed, onComplete is called.
43+
* Uses given BackpressureStrategy.
44+
*/
45+
public static <T> Flowable<T> flowableOneByOne(final Query<T> query, BackpressureStrategy strategy) {
46+
return Flowable.create(emitter -> createListItemEmitter(query, emitter), strategy);
47+
}
48+
49+
static <T> void createListItemEmitter(final Query<T> query, final FlowableEmitter<T> emitter) {
50+
final DataSubscription dataSubscription = query.subscribe().observer(data -> {
51+
for (T datum : data) {
52+
if (emitter.isCancelled()) {
53+
return;
54+
} else {
55+
emitter.onNext(datum);
56+
}
57+
}
58+
if (!emitter.isCancelled()) {
59+
emitter.onComplete();
60+
}
61+
});
62+
emitter.setCancellable(dataSubscription::cancel);
63+
}
64+
65+
/**
66+
* The returned Observable emits Query results as Lists.
67+
* Never completes, so you will get updates when underlying data changes
68+
* (see {@link Query#subscribe()} for details).
69+
*/
70+
public static <T> Observable<List<T>> observable(final Query<T> query) {
71+
return Observable.create(emitter -> {
72+
final DataSubscription dataSubscription = query.subscribe().observer(data -> {
73+
if (!emitter.isDisposed()) {
74+
emitter.onNext(data);
75+
}
76+
});
77+
emitter.setCancellable(dataSubscription::cancel);
78+
});
79+
}
80+
81+
/**
82+
* The returned Single emits one Query result as a List.
83+
*/
84+
public static <T> Single<List<T>> single(final Query<T> query) {
85+
return Single.create(emitter -> {
86+
query.subscribe().single().observer(data -> {
87+
if (!emitter.isDisposed()) {
88+
emitter.onSuccess(data);
89+
}
90+
});
91+
// no need to cancel, single never subscribes
92+
});
93+
}
94+
}

0 commit comments

Comments
 (0)