val temp = null
Observable.just("some value")
.flatMap {
// Throw exception if temp is null
if (temp != null) Observable.just(it) else Observable.error(Exception(""))
}
.onErrorReturnItem("") // return blank string if exception is thrown
.flatMap { v ->
Single.create<String>{
// do something
it.onSuccess("Yepeee $v");
}.toObservable()
}
.subscribe({
System.out.println("Yes it worked: $it");
},{
System.out.println("Sorry: $it");
})
如果遇到null,应该抛出一个错误,然后使用
onErrorReturn{}
onErrorReturnItem()
onError
Observer
因此,您的代码应该如下所示:
RxFirebaseAuth.createUserWithEmailAndPassword(auth, email, password)
.map { authResult ->
user.uid = authResult.user.uid
authResult.user.uid
}
.flatMap<UploadTask.TaskSnapshot>(
{ uid ->
if (imageUri != null)
RxFirebaseStorage.putFile(mFirebaseStorage
.getReference(STORAGE_IMAGE_REFERENCE)
.child(uid), imageUri)
else
Observable.error<Exception>(Exception("null value")) // Throw exception if imageUri is null
}
)
.map { taskSnapshot -> user.photoUrl = taskSnapshot.downloadUrl!!.toString() }
.onErrorReturn {
user.photoUrl = "" // assign blank url string if exception is thrown
}
.map {
RxFirebaseDatabase
.setValue(mFirebaseDatabase.getReference("user")
.child(user.uid), user).subscribe()
}
.doOnComplete { appLocalDataStore.saveUser(user) }
.toObservable()
但这段代码有一个问题,以前发生过任何异常
onErrorReturn
将生成一个空白uri,并导致我们不希望的进一步链执行。如果发生任何其他异常,则应调用一个错误。
. 请看以下代码段:
...
...
.flatMap<UploadTask.TaskSnapshot>(
{ uid ->
if (imageUri != null)
RxFirebaseStorage.putFile(mFirebaseStorage
.getReference(STORAGE_IMAGE_REFERENCE)
.child(uid), imageUri)
else
Observable.error<MyCustomException>(MyCustomException("null value")) // Throw exception if imageUri is null
}
)
.map { taskSnapshot -> user.photoUrl = taskSnapshot.downloadUrl!!.toString() }
.onErrorReturn {
if(it !is MyCustomException)
throw it
else
user.photoUrl = "" // assign blank url string if exception is thrown
}
...
...