I am pretty new to android programming. I've written a code to perform API query using the OkHttp Library.
My question is, once the OkHttp's enqueue process has started, and the user immediately rotates the phone, thereby destroying and restarting the activity, does that cause a second enqueue process to occur, thereby causing the code to perform 2 OkHttp Api query in parallel?
I know that if we are using httpUrlConnections with AsyncTask (instead of OkHttp), this is a known issue. And we can bypass it by using an AsyncTaskLoader, which has a separate life cycle from our MainActivity. But I am just wondering of OkHttp also takes that into account.
Thank you! An example of my code is shown below if that helps in anyway.
private void performApiCall(String url) {
mLoadingPb.setVisibility(View.VISIBLE);
// is network available?
if (NetworkUtility.isNetworkAvailable(MainActivity.this)) {
// create new OkHttp client
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build(); // building the request
Call call = client.newCall(request);
// Asynchronous task
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
alertUserAboutError();
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d(TAG, "OkHttp, onResponse received");
// perform data download
if (response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
String jsonData = response.body().string(); // this is the raw data in json format
Log.v(TAG, jsonData);
try {
if (mMovieData == null) {
saveMovieData(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrentPage++;
setRecyclerView();
Log.d(TAG, "OkHttpCall completed");
}
});
} else {
appendMovieData(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
mCurrentPage++;
updateRecyclerView();
Log.d(TAG, "OkHttpCall completed");
}
});
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, "Exception caught: ", e);
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
alertUserAboutError();
}
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLoadingPb.setVisibility(View.INVISIBLE);
}
});
alertUserNetworkUnavailable();
}
}
My question is, once the OkHttp's enqueue process has started, and the user immediately rotates the phone, thereby destroying and restarting the activity, does that cause a second enqueue process to occur, thereby causing the code to perform 2 OkHttp Api query in parallel?
If your activity is automatically calling enqueue()
when it is created, then yes.
And we can bypass it by using an AsyncTaskLoader, which has a separate life cycle from our MainActivity.
Note that the Architecture Components' support for view-models and LiveData
is Google's current direction for addressing the problems that loaders tried to address.