11
Mb3leb
7y

@Android Question

Does all android devs use Async Task for their Json calls or you prefer to do them on Main Thread ?

Am just asking to improve my skills and get some senior programmers opinion

Comments
  • 1
    No. Asnyc tasks are problematical for certain usecases, and can lead to memory leaks if not used correctly. you can use the Loader pattern, or a background thread/events.

    Also, never do IO on the main thread. Never. Turn on the strict mode to elimate those.
  • 0
    @magicMirror can you please tell me more abou memory leaks in async task? Because am still not sure if i have to do my calls in async task or on the main thread am still doing my research concerning it
  • 3
    I prefer Okhttp3 for Web service calls.

    No need to worry about async task/any loder.

    - Use async method of Okhttp

    - Its good to implement interface to handle result callbacks
  • 2
    @Mba3gar Never do IO on the Main thread. Never. I cannot stress this enough: Only really really bad devs do IO on the Main thread.
    @rakesh OkHttp vs AysncTask...
    same as asking how do you prefer to use a car, some say they use a cabriole, some say racecar, other say a hutchback. But there is is only one who says he uses a v8 engine, and everyone looks at them...
  • 0
    @magicMirror so from now on i will always use async task for my http calls. Thank you
  • 0
    Its not 'prefer', if you do api calls on the main thread, i will personally punch you. Use okhttp or volley or retrofit or whatever.
  • 2
    @magicMirror

    If I didn't understand you wrong. It wasn't about Async vs Okhttp3

    Main problem: Json calls, best practice

    Solution A: MUST use background thread for HTTP calls

    Solution B: SHOULD use okhttp3, as It includes calling a web call and background handling both.

    Im with you on " Never do IO on the Main thread" .
  • 0
    @rakesh Please understand how okhttp3 works before using an OR.
  • 0
    Well don't do anything intensive on the UI thread.

    I have used asynctask for all of the webservice calls. I did however make a small framework for myself to make it easier.
  • 0
    But guys when i already oost this question of stackoverflow they already said that retrofit does never do any calls on the main UI thread by default
  • 1
    Retrofit is a good way, however Android docs recommends using Intent services instead. They are good, if the download size are big.
    I prefer to use AsyncTasks within a singleton class. I have tested this pattern multiple times and I rarely get any Memory leaks.
    Here's few good reads

    https://stackoverflow.com/q/...

    https://marsic.info/2016/03/...

    “Using IntentService vs AsyncTask in Android” @KevinHoffman https://android.jlelse.eu/using-int...

    Also try to follow advice of Commonsware guy on Stackoverflow. He's the top Android developer there by reputation and almost always give you a good advice on anything related to Android. Romanguy is another example.
  • 0
    You can try ReactiveX instead, much better than AsyncTasks https://github.com/ReactiveX/RxJava
  • 1
    @Milenchy you mean rxjava?
  • 1
    @Mba3gar yes, it's ReactiveXJava

    ReactiveX has different iterations for different languages
Add Comment