0

I have the following code in my repository:

class BusRepositoryImpl(
    private val busService: BusService
) : BusRepository 

    override fun get(id: String, callback: Callback<BusItem>){
            AsyncTask.execute {
                try {
                    val apiResponse = busService.get(id)
                    callback.onResponse(apiResponse.bus)
                }
                catch (e: Exception){
                    callback.onFailure(e)
                }
            }
        }
}

busService:

class BusServiceImpl(private val baseUrl: String, private val httpClient: HttpClient) 
     override fun get(id: String): ApiResponse {
           return httpClient.get("${baseUrl}/bus/$id")
     }

ViewModel:

 fun get(id: String) {
        busRepository.get(id, object: Callback<BusItem>{
            override fun onResponse(data: BusItem) {
                busLiveData.postValue(data)
            }

            override fun onFailure(t: Throwable) {
                errors.postValue(t)
            }

        })
    }

It works so far, but I'd like to know if using AsyncTask like this can lead to any problems. I heard about AsyncTask being a bad solution that is soon to be deprecated by Google so I'm in doubt. If theres any other alternative to execute my request on a background thread I would appreciate if you could indicate it.

I dont want to use Retrofit or any similar alternatives.

Alok Bharti
  • 136
  • 1
  • 8
MichelReap
  • 5,630
  • 11
  • 37
  • 99

2 Answers2

1

Using AsyncTask is completely fine actually. It is a part of core Android and it won't be removed in nearest future. However AsyncTask has some drawbacks thus many developers use more "clean" and "powerful" solutions like Coroutines or RxJava. In your case (considering your code written in Kotlin) if you want a modern approach you should consider migrating to Kotlin Coroutines

Emin Guliev
  • 188
  • 2
  • 11
  • thanks. What would happen if my I leave the app while the request is still being perform? will this crash/leak? – MichelReap Jan 18 '20 at 19:24
  • I cant use any of those libraries unfortunately :( – MichelReap Jan 18 '20 at 19:24
  • Wait. Actually I am a little embarrassed. Which pattern and frameworks do you use in your project? – Emin Guliev Jan 18 '20 at 19:36
  • @MichelReap look at this: https://stackoverflow.com/questions/46273753/android-asynctask-memory-leaks – Emin Guliev Jan 18 '20 at 19:47
  • no patterns, no frameworks. Plain vanilla android with lifecycle components – MichelReap Jan 18 '20 at 20:04
  • @MichelReap I've asked because there is a field suggesting that you are using LiveData. Okey. LiveData is indeed a part of MVVM pattern and it is generally (if not always) should be used within that pattern. So again we are coming to a question whether you need a modern approach or just get your code working somehow. If latter then there is no guarantee that this code will work as intended. If we come back to your first comment then you should look into links I've provided. I actually don't understand your initial question as you cannot include other libraries – Emin Guliev Jan 18 '20 at 22:42
  • There are more ways to do background processing in vanilla Android, wihtout libraries. Im just asking which one would be more suitable here. – MichelReap Jan 19 '20 at 06:56
0

I see you are a beginner in android development. I recommend using kotlin in your project. Please check the google sample Also you can check the articles viewModel sample and retrofit + corutines

Alexander
  • 511
  • 4
  • 14