Progress Dialog is paused - android

Here it is.
Before fetching the data, creating the progress dialog and showing it now fetching data from server using volley library. It working fine. After that I need to send the data to local database as follow:
if (responseStatusCondition) {
JSONArray jsonArray=response.getJSONArray("response");
for (int i=0;i<jsonArray.length();i++) {
JSONObject childJSONObject = jsonArray.getJSONObject(i);
// Set the flag bit to 1 since data is already at server
long result = bloodPressureDatabaseFragment.insertIntoBloodPressureDetails(
childJSONObject.getInt("systolic"),
childJSONObject.getInt("diastolic"),
childJSONObject.getLong("timestamp"),1 );
}
}
Now problem is this, since the fetching part is happening in background is ok. But the insertion data into db part is happening on the Main UI which leads to pause the progress dialog. Is there any way to solve this problem.
I have done insertions part through the AsynckTask. Is there any alternative to do this task. I also read we can this through the use of Async-Await.
It is good if you please comment the reason of downvoting the question.

Related

Android realm findAllAsync() freezing UI

when I'm querying lot of elements the UI get freeze.
realm.where(TVRealm.class).equalTo("favorite", true).findAllAsync()
.addChangeListener(new RealmChangeListener<RealmResults<TVRealm>>() {
#Override
public void onChange(RealmResults<TVRealm> element) {
List<TV> tvList = new ArrayList<>();
for (TVRealm tvRealm : element) {
tvList.add(prepareTV(TVRealm.toTV(tvRealm), true));
}
listener.onDataChange(tvList);
}
});
I thought that findAllAsync() will run on other thread and avoid the issue but not.
Does anyone knows how to avoid this issue? Maybe there is another way without using findAllAsync() method.
Thanks.
According to official Realm document, findAllAsync works in a background thread.
https://realm.io/docs/java/latest/#asynchronous-queries
I think your data changes too often and so you're trying to notify ui too often. So you're blocking ui. I guess you are notifying a RecyclerView adapter in your onDataChange method.
Also if your result list has too many items, every time when your data changed exploring the results and adding items to a new list may block ui.
If I don't make mistake, while RealmResults is updating, for each change you create new collection with new models and update UI. Try to call findAll() in another thread, map results to TVs and post completed list of TVs to UI thread.
why dont you try this
RealmResult<item> res=realm.where(item.class).where("name","sanjay").findAll();
it will get all the data at a time.

Android ListView only shows data after app restart

I'm trying to fill a list view with data i'm receiving from and odata service in json format. The data is already fetched and can be accessed with getIODataEntry().
I'm appending the respective values to a string to see an output in LogCat and split the string afterwards to fill my listView with the single values.
ListView listView_CarrierCollection = null;
...
private void showData() {
Log.d("debug", "log 1");
String carrierCollection = "";
for(int i=0; i<getIODataEntry().size(); i++) {
carrierCollection += getIODataEntry().get(i).getPropertyValue("Carrname");
carrierCollection += ";";
}
Log.d("debug", carrierCollection);
ArrayList<String> list = new ArrayList<String>(Arrays.asList(carrierCollection.split(";")));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.custom_textview, list);
listView_CarrierCollection.setAdapter(arrayAdapter);
Log.d("debug", "log 2");
}
everything works, but only after my devices' screen has turned off and has being turned on again or after i close and reopen the app. The logs instead are prompted instantly to logcat, but the listView only contains the data after reopening the app.
Am i missing something out? Every hint would be appreciated!
EDIT:
Finally got it to work - turned out it had nothing to do with the posted code above. I had some listeners, that were trying to update a TextField in a Fragment. It seems as if MyFragment.textfield.setText("test"); blocked any further operations in some way. Surrounding it with runOnUiThread(new Runnable(){ ... }) solved it for me and got the whole thing to work.
Nevertheless thank you for your ideas and help!
Late to the party but may help someone else in the future
In my case, I wasn't calling notifyDataSetChanged() when i was updating my mutable array list. Therefore it was only working when i was tuning sceen off and on again. But after calling notifyDataSetChanged() it worked as usual.
Hope it helps ;)

Android - running background tasks that can fail

I need to be able to cancel tasks involving web requests.
but I got some issues of memory management and exception handling when something fail.
For example:
I want to create a text edit for searching apps in user device.
so whenever user end a key, I want to clear current search task, and restart search.
The problem I got is in android 4.4, when trying to load the label of the app (to get it's name) I get an exception.
Also of I try to search contacts I'm getting an invlalid uri, fo rcontact photo.
I don't want any help dealing with this errors, I want a solution that will help me ignore this erros (try catach dont work) currently the entire ui get's stuck and app crash.
I'm using the executors service and call cancel when user gives me an input. but it's not enough.
Any advice will be appreciated, thanks.
if you're using AsyncTask i would like to suggest you to make it like this
AsyncTask<Void, Void, Void> a = new AsyncTask<Void, Void, Void>(){
#Override
protected Void doInBackground(Void... params) {
return null;
}
};
a.execute();
// at some point you want to cancel just use
a.cancel(true);
You can even put tasks together in List or array and loop to cancel it whenever you want.
You can try like this: what you want is getting result in background by network and show result in view ,you can Register a Listener to one task,. you can callback method of listener to use show result in view if it get the results in background ,if you do not need the result of current task and want satrt the next task ,you can only remove listener of current task,so the view will not reload
This is my idea ,I am so sorry for my bad english , if you do not see ,you can send message to me, and my email is bjltxsp#gmail.com

load data into an array using for loop android

I am trying to populate an array of image urls from Post class but the activity returns a blanked page. am i doing it right. sorry am learning android don't know much.
ArrayList PostDetails = new ArrayList();
for (int index=0;index<imageUrls.length;index++){
imageUrls[index]=PostDetails.get(index).getImag();
}
You should wait till the task is completed, you can put the last part in onPostExecute method..
protected void onPostExecute( ) {
String[]imageUrls= new String[PostDetails.size()];
for (int index=0;index<imageUrls.length;index++){
imageUrls[index]=PostDetails.get(index).getImag();
}
}
The answer by Nunu is correct, but I would like to expand on it a little. The assumption is that loadingTask is an AsyncTask, thus when you call execute the code within doInBackground will execute asynchronously. The code where you create and populate the array will execute in parallel to your population of PostDetails, which I assume is populated within the loadingTask. Since the array loop will execute very quickly, it will have completed long before the network operation performed by loadingTask. Thus your page is blank because you have looped through the array before PostDetails has been populated.
The solution is just as Nunu suggests, which is to move the population of the imageUrls array into the onPostExecute method of your AsyncTask. Also, any UI manipulation that uses this array should also be moved into onPostExecute.

Android application acting crazy after coming from background

I am displaying search results from a webservice.
What I do is OnCreate I hit the webservice display records, as android supports multitasking. If user opens another screen and after some time comes back to the search results page, the application starts acting crazy....
OnCreate method I load data some thing like :
private void loadData() throws Throwable{
try {
jsonArray = JSONService.getJsonArray(getResources().getString(R.string.catJson));
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
throw e;
}
}
and then I iterate through json array and change labels value to display result on the screen.
Any ideas how to fix this?
Yes, please explain our problem better. Are you familiar with the Activity life-cycle? There are several different callbacks that you must manage in order for your concept of multitasking to actually work. Based on the limited info you've provided, I don't think you're saving any of your application state when your Activity loses focus. So your process may be getting shutdown and when you come back, your JSON array is gone. Read this:
http://developer.android.com/guide/topics/fundamentals.html#lcycles
Are you calling the super in your OnCreate?
super.onCreate(savedInstanceState);

Categories

Resources