views:

71

answers:

1

Hello,

My problem is that I had a program working before, without threading, and it took a long time to process info (16 seconds to get XML data and display it). Now I've gotten the whole threading and async thing down, but for some reason it is making my program SLOWER (in the emulator, device is not available right now), is there anything I've done that could have caused this, the UI thread is fine, but my async thread takes a minute and a half to execute. (when I had it in the UI thread used to take only 16 seconds, but froze the UI thread)

Here is the code for my thread, it sits inside of the main class:

    private class TeamSearchTask extends AsyncTask<String,Void,String> {

    CharSequence nfo;
    String [] matches;
    String [] data;
    String teamNum;
    ProgressDialog loading;

    protected void onPreExecute()
    {
        //Show the 'loading' dialog
        loading = new ProgressDialog(SapphireAlliance.this);
        loading.setMessage("Loading, please wait...");
        loading.show();
    }

    protected String doInBackground(String... teamNumber) 
    {
        try
        {
            //Team information ------------------------------------------------------------------------------------
            teamNum = teamNumber[0];        
            //Array of team data
            data = APIconnection.getTeams(teamNum, "");

            //Display basic team info
            nfo = ("\nFormal Team Name:\n" + data[1] + 
                    "\n\nLocation:\n" + data [3] + ", " + data[4] + ", " + data[5] +
                    "\n\nRookie Year:\n" + data[6] +
                    "\n\nRobot Name:\n" + data[7] +
                    "\n\nWebsite:\n" + data[8] + "\n\n\n\n\n\n\n\n\n"); 

            //Make match archive --------------------------------------------------------------------------------------

            String [] events = APIconnection.getEventIdsByYear(year1);
            ArrayList<String> matches = new ArrayList<String>();
            for (int i = 0; i<events.length; i++)
            {
                String [] add = APIconnection.getMatches2(teamNum, events[i] ,"","");
                for(int j = 0; j<add.length; j++)
                    matches.add(add[j]);            
            }
            String [] out = new String [matches.size()];
            matches.toArray(out);
            return "";
        }
        catch(Exception e)
        {
            return e.toString();
        }
    }

    protected void onPostExecute(String result) {
        if(result.equals(""))
        {
            info.setText(nfo);
            matchArchive(matches);

            //title 
            CharSequence ttl = "Team " + teamNum;
            titlets.setText(ttl.toString());
            loading.dismiss();
        }
        else 
        {
            alert.setMessage(result);
            alert.show();
        }
    }
}

Anything in there that could be causing this? :|

A: 

It may be that your thread priority may not be set very high. I remember the default is rather low. However, for what you're doing, it shouldn't be taking more than a couple seconds. I would think that the real problem lies in APIconnection going to the network and doing something that takes a long time. Particularly, that for loop that does the event fetching would be doing a lot of work if each call opens a new socket, assuming that this is for FIRST matches. :P

Quartz
Yes! It is for FIRST matches! <3 xD Is there a way to raise the priority? My program runs extremely well and quickly other times and ran actually FASTER when it was on the UI thread!
Nick
Hm. I thought there was a way to do that for AsyncTask, but I guess not. I think your real problem is either how you're connecting to the data source or how you are parsing the XML. 16 seconds on a main UI thread seems a little slow for that sort of operation.
Quartz
Is it maybe because I'm using : javax.xml.parsers.DocumentBuilderFactory; javax.xml.parsers.DocumentBuilder; org.xml.sax.SAXException; org.xml.sax.SAXParseException; those instead of android.sax?
Nick
The particulars of the API you use usually isn't important. The flow of the algorithm is more likely the problem. Without specific code, there's not much more I can help you with. I'd recommend looking at how many network calls you're making, how much memory is being used, and the flow of your XML processing routine. If your processing is truly linear time, then you're fine. But it sounds like there's some bottleneck you're overlooking. Try using [traceview](http://developer.android.com/guide/developing/tools/traceview.html) to profile your code.
Quartz
Ok, I've inspected and the cause of some of my problems was repetition of the execution of the tasks (for some reason it executed it 3 times-ish)Now the load times are cut down to 30 secs (still a lot, but a lot faster than before, and a lot more responsive)I could never get the traceview thing working but I don't need it (for now at least).
Nick