We are trying to remove records from Couchbase using async().remove(key)
where key is the document to be removed.
We are sending a list of keys using Observable.from(documents).flatMap(new Func1<String, Observable<JsonDocument>>()
. The list contains some values which are not present in Couchbase Bucket. For that purpose we added onErrorResumeNext(Observable.<JsonDocument> empty()).last().toBlocking().singleOrDefault(null)
.
However, when the remove function encounters a key not in Couchbase, it throws an Exception stating NoSuchElementException:Sequence Contains No Elements
. Even after using onErrorResumeNext
, we are getting the exception. What could be the cause ?
That’s because of the last()
operator. You should:
- Perform both
bucket.async().get(k)
andonErrorResumeNext(Observable.empty())
inside theflatMap
, but nothing more - Just after the
flatMap
, simply dotoBlocking().lastOrDefault(null)
.
This way you won’t get the exception and will receive the last document removed (should match the last key in the collection), or null only if the collection is empty or every key didn’t map to a stored document.