android - RxJava: how to return the correct type of null -


i'm developing app using firebase, kotlin , rxjava.

basically, need register user using auth firebase, if user selected photo, upload photo , save user in database firebase.

until have this

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                             maybe.empty<uploadtask.tasksnapshot>()                     }             )             .map { tasksnapshot -> user.photourl = tasksnapshot.downloadurl!!.tostring() }             .map {                 rxfirebasedatabase                         .setvalue(mfirebasedatabase.getreference("user")                                 .child(user.uid), user).subscribe()             }             .dooncomplete { applocaldatastore.saveuser(user) }             .toobservable() 

it's working correctly when user selects photo, when it's not selected, other maps ignored, because returned maybe.empty().

how should implement work or without user photo?

thanks.

have @ following construct:

        val temp = null         observable.just("some value")                 .flatmap {                     // throw exception if temp null                     if (temp != null) observable.just(it) else observable.error(exception(""))                  }                 .onerrorreturnitem("") // return blank string if exception thrown                 .flatmap { v ->                     single.create<string>{                         //                         it.onsuccess("yepeee $v");                     }.toobservable()                 }                 .subscribe({                     system.out.println("yes worked: $it");                 },{                     system.out.println("sorry: $it");                 }) 

you should throw error if encounter null, , use onerrorreturn{} or onerrorreturnitem() operator return default value passed next operator chain without jumping onerror in observer.

so code should this:

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 null                         }                 )                 .map { tasksnapshot -> user.photourl = tasksnapshot.downloadurl!!.tostring() }                 .onerrorreturn {                     user.photourl = "" // assign blank url string if exception thrown                 }                 .map {                     rxfirebasedatabase                             .setvalue(mfirebasedatabase.getreference("user")                                     .child(user.uid), user).subscribe()                 }                 .dooncomplete { applocaldatastore.saveuser(user) }                 .toobservable() 

but there problem code exception occurred before onerrorreturn yield blank uri , result in further chain execution not want. if other exception occurs onerror should invoked.

for need create custom exception class , catch thrown exception in onerrorreturn. have @ following snippet:

... ...  .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 null                         }                 )                 .map { tasksnapshot -> user.photourl = tasksnapshot.downloadurl!!.tostring() }                 .onerrorreturn {                     if(it !is mycustomexception)                         throw                     else                         user.photourl = "" // assign blank url string if exception thrown                  } ... ... 

hope helps.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -