In Android, how can I separate the data load into its own thread?

advertisements

So I'm working on my "hello world" application in android/java, and elected to do a sports app (which is strange...I don't like sports...but whatever). So I set up my layout, allow users to 'drill down', so they can see the layout for Baseball, or MLB, or the Indians. Say a user selects 'Indians' from the MLB view. I update the tabs, potentially the color scheme, background, etc, and load the data for the 'news' and 'players' tabs (the latter of which is unique to team layouts). Unfortunately, api calls can sometimes take relatively long to complete, especially when the free API from ESPN is capped at 1 call per second. I do some significant caching already, but there's no way I can guarantee that I won't be loading both 'news' and 'players' for 'Indians' at the same time, so one of the requests will have to wait a full second to return.

So my solution is to have a data loading thread - the UI says 'get me this data', and does the UI work not contingent on the data being there. The question though is - once the data is returned from the data loader (as each piece comes back), how should it update or notify the UI appropriately? My current thought is:

UI thread:

OnSelectIndians()
{
  DataLoadThread.GetIndiansPlayers();
  DataLoadThread.GetIndiansNews();
  // UI stuff
}

OnPlayersLoaded(Array Players)
{
  if (layout == INDIANS_LAYOUT)  // Make sure we haven't changed layouts
  {
    foreach player in Players
      tab[PLAYERS].textview.text += player
  }
}

But this isn't a problem I've had to deal with before. Is this the right way to go about it? Or is there a better/easier design I can use? I don't particularly like requiring the UI thread to have a 'on data returned' method for every type of data I can request. My other loosely-formed idea is to create a lambda function in the UI code, which is passed to the data loader and executed in the data loading thread, so:

DataLoadThread.Queue(
    foreach player in GetIndiansPlayers()
        myView.tab[PLAYERS].textview.text += player;
);

But I think this is probably the worse route, as now we have 2 threads interacting with the UI. Any advice?

Edit: Okay I got it working using AsyncTask. Out of the box, it still has the problem listed above that I would have to create a new derived class for every type if data I load (so PlayerLoadTask, NewsLoadTask, StandingsLoadTask, etc etc). I also wanted was to have most of the logic visible during the call, so if I'm looking at the event code I know what its doing. Below is the working implementation - would appreciate any feedback on it, but I'll accept the first answer below just the same.

abstract public class LoadDataHelper { public LoadDataHelper(DataLoader dl, Object param) { mDataLoader = dl; mParam = param; }

    abstract public LinkedList<String> LoadData();
    protected DataLoader mDataLoader;
    protected Object mParam;
}

abstract public class UpdateUIHelper {
    public UpdateUIHelper(MyActivity context) {
        mContext = context;
    }

    abstract public void UpdateUI(LinkedList<String> results);
    protected MyActivity mContext;
}

private class LoadDataTask extends AsyncTask<Void, Void, LinkedList<String> >   {
    private LoadDataHelper mLdh;
    private UpdateUIHelper mUih;

    LoadDataTask(LoadDataHelper ldh, UpdateUIHelper uih) {
        mLdh = ldh;
        mUih = uih;
    }

    @Override
    protected LinkedList<String> doInBackground(Void... params) {
        return mLdh.LoadData();
    }

    @Override
    protected void onPostExecute(LinkedList<String> results) {
        mUih.UpdateUI(results);
    }
}

//
// .....
// 

    LoadDataTask task = new LoadDataTask(new LoadDataHelper(mDataLoader, "football") {
                                            public LinkedList<String> LoadData() {
                                                return mDataLoader.LoadLeaguesFromSport((String)mParam);
                                            }
                                        },
                                        new UpdateUIHelper(this) {
                                            public void UpdateUI(LinkedList<String> results) {
                                                TextView tv = (TextView)findViewById(R.id.tv1);
                                                tv.setText("");
                                                for (String res : results) {
                                                    tv.append(res + "\n");
                                                }
                                            }
                                        });
    task.execute();


Take a look at:

1) AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html

The AsyncTask.onPostExecute will be executed in the UI thread. I think this is the most common technique to do background processing.

2) runOnUIThread: If you are managing your own worker thread, you can use this in a worker thread to make sure code is run on the UI thread.