Code Monkey home page Code Monkey logo

Comments (4)

skydoves avatar skydoves commented on June 6, 2024 1

The second one sounds more makes sense. You can also replace the onError and onException with onFailure if you don't need to handle each case, such as UnKnownHostException, network errors, etc.

apiResponse.onSuccess {
  // TODO: update to LiveData.
}.onFailure { // this actually handles the error and exception cases at the same time.
  // TODO: show error message
}

from sandwich.

skydoves avatar skydoves commented on June 6, 2024

Hey @chanjungkim, thanks for leaving this question.
In this case, I would recommend you utilize the suspendMapSuccess in the new release (1.3.3) like the below:

    loginAndRegister(orderData.phoneNumber).apply { emit(this) }
      .suspendMapSuccess {
        val jwt = "jwt ${data.auth.token}"
        Client.saveAuth(data)
        cancelOrder(
          orderData.orderId,
          OrderCancelRequestBody("store", "re-checkout")
        ).apply { emit(this) }.getOrThrow()
      }
      .suspendMapSuccess { addMultiCartItem(jwt, orderData.cartItems).apply { emit(this) }.getOrThrow() }
      .suspendMapSuccess { checkOut(orderData.checkoutBody!!).apply { emit(this) }.getOrThrow() }
      .suspendOnSuccess { emit(this) }

I'm not sure if this works with your intend, but please try and let me know if you have further questions about this. Thanks!

from sandwich.

skydoves avatar skydoves commented on June 6, 2024

Additionally, if you need to check the sequential responses must succeed and not should throw any exceptions about your request failure, you can also utilize getOrNull() rather than getOrThrow().

  .suspendMapSuccess { addMultiCartItem(jwt, orderData.cartItems).getOrNull() }
      .suspendOnFailure { emit(this) }
      .suspendMapSuccess { 
         if (this != null) {
           checkOut(orderData.checkoutBody!!).getOrNull()
         }
 }}

And just ignore the ApiResponse<Unit> type on the subscriber's side of the flow.

from sandwich.

chanjungkim avatar chanjungkim commented on June 6, 2024

Thank you for the detail answer.

I upgraded the library to 1.3.3 and changed my code as you suggested.

And the flow's emit type is ApiResponse<Any>>

I'd like to use onSuccess, onFailure, onError in the ViewModel. Is that possible?

For example,

In ViewModel.

What I expected.

    // this function is called from Fragment.
    fun reCheckout(){
        shopRepository. reCheckout(orderData: OrderData).onSuccess{
            // TODO: update to LiveData.
        }.onFailure{
            // TODO: show error message
        }.onError{
            // TODO: show error message
        }.onException{
            // TODO: report exception 
        }
    }

Or if I use flow.

    // this function is called from Fragment.
    fun reCheckout(){
        shopRepository. reCheckout(orderData: OrderData).collectLatest{ apiResponse ->
            when(apiResponse){
                  is ApiResponse<CustomAuthData>,
                  is ApiResponse<OrderCancelResponseBody>,
                  is ApiResponse<CartListResult> -> {
                      apiResponse.onFailure{
                        // TODO: show error message
                      }.onError{
                          // TODO: show error message
                      }.onException{
                          // TODO: report exception 
                      }
                  }
                  is ApiResponse<CheckoutResponseBody>->{
                    // TODO: correct proccess
                    apiResponse.onSuccess{
                        // TODO: update to LiveData.
                    }.onFailure{
                        // TODO: show error message
                    }.onError{
                        // TODO: show error message
                    }.onException{
                        // TODO: report exception 
                    }
                }
            }
        }
    }

Should I do in this way..?

from sandwich.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.