Keeping program responsive

Put your problem here if it does not fit any of the other categories.

Keeping program responsive

Postby confused » Tue Jan 13, 2009 2:03 am

Hi all,
in my irc app, I'm trying to improve the responsiveness when it has to deal with a lot of data at a single time.

At the moment, once the data is append()-ed to the textview, I post a delayed runnable (100ms delay) back to the UI thread to make it scroll right down to the bottom. Unfortunately, without the runnable being posted, the ScrollView doesn't correctly calculate the amount it should scroll.

This works fine for normal situations and the program is fully responsive. But when, there is a lot of channel activity, the queue of Runnables start to strain the UI thread and it has trouble appending the data as it arrives and occasionally causes an ANR.

My question is, how would I get around this? Without the Runnable, it doesn't scroll correctly but with it, it causes problems. A possible solution could be to detect when there is an influx of data and post the Runnable less often, but I'm not sure how I would detect this.

Any tips?
confused
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Oct 03, 2008 2:30 pm

Top

Postby MrSnowflake » Tue Jan 13, 2009 2:37 pm

You should also run the append on the ui thread. And then you can do both at the same time: first append the text to the textView and in the same runnable, scroll down. With this I am assuming you handle the networking in a different thread.
ie (in thread):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. static String stringToAdd = some string...
  2.  
  3. mActivity.runOnUiThread(new Runnable() {
  4.  
  5.   public void run() {
  6.  
  7.     mActivity.mTextView.append(stringToAdd); // or whatever the method is
  8.  
  9.     mActivity.mTextView.scrollDown();
  10.  
  11. });
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests