Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle retry request? #5

Open
fjr619 opened this issue Dec 17, 2019 · 6 comments
Open

How to handle retry request? #5

fjr619 opened this issue Dec 17, 2019 · 6 comments

Comments

@fjr619
Copy link

fjr619 commented Dec 17, 2019

with this pattern, it is really easy to use, but i have a problem to retry call API when got error, because we already have livedata in repository and calling api in datasource, how we can get retry request with this pattern?

@mfsi-prateeksharma
Copy link

Hi @fjr619
For that, you need to change the approach of initializing the live data in viewmodel from lazy to var or val,
var example: () -> LiveData<Result<SampleResponse>> = { repository.sample(createRequest()) }

@fjr619
Copy link
Author

fjr619 commented Dec 18, 2019

var example: () -> LiveData<Result<SampleResponse>> = { repository.sample(createRequest()) }

with this approach how we trigger that from button in activity or fragment?
i'm new in kotlin can you explain about this part
var example: () -> LiveData<Result<SampleResponse>> what is that ?

@mfsi-prateeksharma
Copy link

@fjr619 no problem,

viewModel.example().observe(viewLifecycleOwner, Observer { result ->
            when (result) {
                is Result.Success -> {
                    progressBar.visibility = View.GONE
                    Timber.d("response === %s", result.data.email)
                     }
                is Result.Loading -> {
                    progressBar.visibility = View.VISIBLE
                }
                is Result.Error -> {
                    progressBar.visibility = View.GONE
                }
            }
        })

Let me know if it works for you.

@fjr619
Copy link
Author

fjr619 commented Dec 18, 2019

so i create a function for observe in activity

private fun subscribeUI() {
viewModel.example().observe(viewLifecycleOwner, Observer { result ->
            when (result) {
                is Result.Success -> {
                    progressBar.visibility = View.GONE
                    Timber.d("response === %s", result.data.email)
                     }
                is Result.Loading -> {
                    progressBar.visibility = View.VISIBLE
                }
                is Result.Error -> {
                    progressBar.visibility = View.GONE
                }
            }
        })
}

then in my activity will become

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

       subscribeUI() //this is for first time request
       btnRetry.setOnClickListener {
           subscribeUI() //this is for retry request
      }
}

is it correct?

@mfsi-prateeksharma
Copy link

mfsi-prateeksharma commented Dec 18, 2019

@fjr619 yes, I assume you have everything initialized and injected as per the project.
In case you still get the issue, you can provide me your sample project at [email protected] or create a private repo

@fjr619
Copy link
Author

fjr619 commented Dec 18, 2019

my other question, i assume with MVVM it will resolve change orientation problem, but when i rotate my phone, it will trigger calling API again, how to solved it? I read that in ViewModel we can use

init {
   callAPI()
}

to solve the problem, it doesn't with LEGO-Catalog example pattern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants