Skip to content

Commit

Permalink
Merge pull request #12 from btrautmann/feature/set-and-merge
Browse files Browse the repository at this point in the history
Allow for merging a document on set, update version.
  • Loading branch information
Brandon Trautmann authored May 28, 2018
2 parents 00903df + 229182f commit 5257feb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
4 changes: 2 additions & 2 deletions publishing-config.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
// Publishing
libraryVersion = '1.0.4'
libraryVersionName = '1.0.4'
libraryVersion = '1.1.0'
libraryVersionName = '1.1.0'
bintrayRepo = 'maven'
publishedGroupId = 'com.oakwoodsc.rxfirestore'
libraryDescription = 'An RxJava2 wrapper for Cloud Firestore'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,25 @@ public static <T> Observable<QueryObjectResponse<T>> queryObject(@NonNull Docume
@NonNull
@CheckResult
public static <T> Completable set(@NonNull DocumentReference reference, @NonNull T value) {
return Completable.create(new SetOnSubscribe<>(reference, value));
return Completable.create(new SetOnSubscribe<>(reference, value, false));
}

/**
* Creates or merges a document at the given {@link DocumentReference}
* <p>
* Note, this is a <b>blocking</b> call because of how Firestore handles offline persistence.
* That means the onComplete() callback will not be called if the user is offline, so it is recommended
* not to block the UI until this completes
*
* @param reference the {@link DocumentReference} to create or set the document at
* @param value the object to place at the {@link DocumentReference}
* @param <T> the type of the model
* @return {@link Completable}
*/
@NonNull
@CheckResult
public static <T> Completable setAndMerge(@NonNull DocumentReference reference, @NonNull T value) {
return Completable.create(new SetOnSubscribe<>(reference, value, true));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.SetOptions;

import io.reactivex.CompletableEmitter;
import io.reactivex.CompletableOnSubscribe;
Expand All @@ -19,9 +20,12 @@ public class SetOnSubscribe<T> implements CompletableOnSubscribe {

private final T value;

public SetOnSubscribe(DocumentReference reference, T value) {
private final boolean merge;

public SetOnSubscribe(DocumentReference reference, T value, boolean merge) {
this.reference = reference;
this.value = value;
this.merge = merge;
}

@Override
Expand All @@ -41,7 +45,12 @@ public void onComplete(@NonNull Task<Void> task) {
}
};

reference.set(value).addOnCompleteListener(listener);
if (merge) {
reference.set(value, SetOptions.merge()).addOnCompleteListener(listener);
} else {
reference.set(value).addOnCompleteListener(listener);
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ fun <T> DocumentReference.queryObject(objectClass: Class<T>): Observable<QueryOb
fun <T> DocumentReference.setDoc(value: T): Completable =
RxFirestoreDb.set(this, value)

fun <T> DocumentReference.setAndMergeDoc(value: T): Completable =
RxFirestoreDb.setAndMerge(this, value)

fun DocumentReference.updateDoc(updates: Map<String, Any>): Completable =
RxFirestoreDb.update(this, updates)

Expand Down

0 comments on commit 5257feb

Please sign in to comment.