Where should Network calls be made in Android [closed] - android

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
In which situation is it acceptable to make network calls on the UI thread? Or we can say that Network calls should never be on the main UI thread.

Straight from Android documentation:
Perform Network Operations on a Separate Thread
To avoid creating an unresponsive UI, don't perform network operations on the UI thread. By default, Android 3.0 (API level 11) and higher requires you to perform network operations on a thread other than the main UI thread; if you don't, a NetworkOnMainThreadException is thrown.
It is never acceptable to make network calls on the UI thread.

Related

Updating UI from a background thread is a bad practice why? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
My professor told us in the class that updating a UI element from a background task is a bad practice because it might hang our UI, I can't understand why, can someone please elaborate
Background threads are not aware of your ui state , in simple words they are not aware if your ui still exists or not , in this case if you try to update your ui from the background thread and if by any chance your ui element doen't exist at that time, your app will misbehave (unexpected behaviour) or CRASH .
So to avoid this unexpected behaviour, we only update the ui from the main/ui thread.

Long tasks in Application class [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Is it a good practice to make long tasks such as server requests in the application class? Let's say these requests are for initialization,is it still fine to place these requests in the oncreate method in the Application class.
It is not. Everything inside the onCreate of Application class will be executed in the main thread, resulting in freezing the UI if your task takes a lot of time.
The best practice, when it comes to operations such as communicating witha server, is to implement a Repository Pattern and execute the time consuming operations in a different thread, then use the results in the UI Thread.

How Retrofit library working faster than default AsyncTask? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Today i used retrofit library in my project instead of AsyncTask because it is faster than HttpUrlConnection but i am wondering how it is faster and what mechanism they are using. I searched about that but didn't get the accurate or satisfactory answer. Please help me out to understand the concept behind it.
Async task execute serially and are single threaded by default and they will wait for last call to complete before execution of next one and designed in a way so that to avoid common errors due to parallel running threads. Retrofit is not. It sends calls parallely and use ThreadPoolExecutor.

Sending requests in background android [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
What is the best way to send requests sequentially in background? I'm having trouble deciding between an AsyncTask, Service or Runnable due to the complexity of my problem. Basically I have a DB, with pending requests, and I want to create a "background job" that doesn't get killed until he finishes his task. This job needs to check if there're any pending request and if yes, send them first. For this I'm planning to have a background task with a queue that consumes the first element and continues the process on success and aborts the operation on failure. It also needs to be a singleton so, if any request arrives while it's still sending pending requests, I can guarantee the sequencial state integrity. Said that what do you guys think is the best android class that will suit my problem? Thanks
I thinking about using this. It looks like a very nice framework to do what I'm looking for https://github.com/path/android-priority-jobqueue

Why does not allow network access in the main thread of a process in Android? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
can you explain why does not allow network access in the main thread of a process in Android?
If anybody can explain. Its will be helpful for me.
Because the main thread is responsible for all the UI (user interface) operations (it is also called the UI thread). Everything related to displaying stuff in the screen is done by it. So if you occupy the main thread with long operations such as a network operation, you will experience jitter in the screen and even ANR errors (Application Not Responding). In more recent versions of Android you will not even be allowed to do that as the application throws NetworkOnMainThreadException.
Till Donut it is used to create multiple threads from 1.6-2.3 that you was using before but if now you are using 3.0 or above then it is used to create single threaded model by using AsyncTask otherwise you ll get the NetworkOnMainThreadException.

Categories

Resources