This is how we can use Retrofit with Kotlin Flow in Android. Making multiple network calls in parallel using Retrofit with Kotlin Flow.Making network calls in series using Retrofit with Kotlin Flow. I must mention that you can learn much more from the GitHub repository that I mentioned above in this blog. This way, we are able to fetch the data from the network using Retrofit with Kotlin Flow in Android. You can find the complete code for the implementation mentioned in this blog in the project itself.įirst, we need to set up our dependencies for the Retrofit as below:Ĭlass SingleNetworkCallViewModel ( private val apiHelper : ApiHelper, private val dbHelper : DatabaseHelper ) : ViewModel ( ) Part 4: How to implement Dependency Injection in your app with Dagger 2. Part 3: All about that Architecture: exploring different architecture patterns and how to use them in your app. and when I try to use the execute method instead I'm facing a NetworkOnMainThreadException error. However, since you are generating a MutableLiveData, you should be able to observe, which will give you an update when your asynchronous call sets the value. 1) The enqueue method is working async and the result value will always return empty or fail or whatever. Part 2: How to start building your Android app: creating Mockups, UI, and XML layouts. 4 Answers Sorted by: 6 You're making an asynchronous call, so data.value will not be set until that asynchronous call resolves. The project follows a basic MVVM Architecture for simplicity. Part 1: An introduction to the SOLID principles. Comments are added inside the code to understand the code in more detail. Navigate to the app > java > your app’s package name > Right-click on it > New > Java class select it as Interface and name the file as RetrofitAPI and add below code to it. I will be using the following project for the implementation part. Step 6: Creating an Interface class for our API Call.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |