RATCHET

RATCHET Labs

05/26/2011

Android: AsyncTask, UI Threading made easy

AsynkTask is a class that allows for easy use of UI Threading. AsyncTask allows you to preform background tasks in a separate thread with out disrupting the UI thread. In ShotShakr 2.0 I am using AsyncTask to filter the list of shots when a filter is selected. In order to use AsyncTask it must be subclassed and override at least one method (doInBackground(Params… )).

Here is ShotShakr’s UpdateShot Class. Here i am passing in the list of filters and shots.

private class UpdateShots extends AsyncTask<String, Void, ArrayList<Shot>> {

	    	private ArrayList<String> aFilters;
	    	private ArrayList<Shot> aShots;
	   	
	    	public UpdateShots(ArrayList<String> filters, ArrayList<Shot> allShots){
	    	  	aFilters = filters;
	    		aShots = allShots;
	    	}
	   	
	   		
	    	
	    }

doinbackground method does the work of the filtering and after completion postExecute gets fired. Here is where i update the UI Thread with the results.

	@Override
	    	protected void onPostExecute(ArrayList<Shot> result) {
	    		//Update UI Thread
	    		shotpicker.this.filteredShots = result;
	    	}


	    	@Override
	    	protected ArrayList<Shot> doInBackground(String... params) {
	    		ArrayList<Shot> newShots = new ArrayList<Shot>();
	   		
	    			//filter shots
                                // add to new list
	   		
	   		//sends to postexecute
	    		return newShots;
	    	}

Here is how to fire of the AsyncTask

new UpdateShots(filters,allShots).execute();

See a more detailed how to http://developer.android.com/reference/android/os/AsyncTask.html

Cheers

  • Facebook
  • Twitter
  • Digg
  • Print
  • email

11/16/2010

Javascript Shorthand – a quick refresher

Legacy Javascript is great. It’s readable, functional, and normally intuitive to decent developer. On the flip side, with today’s emphasis on website performance, every byte of data you include in a Javascript file or function degrades the speed at which the browser will render the content.

We can (and will, in a later post) debate the merits of differing methods of lazy-loading files, the Sizzle engine which powers jQuery, and mini-fying your code. Today I deal with a quick and easy way to squeeze the same amount of functionality into a smaller file-size – Javascript Shorthand.

While there is not an official definition of javascript shorthand defined by any of the standards-governing bodies, there are quite a few tricks that don’t trigger a ‘fail’ in the javascript validators:

1. Variable manipulation shorthand – add/multiply/subtract/divide a variable in one line
2. Ternary operators – conditional statements
3. Default variable assignments
4. Associative array shorthand

Examples:

/*
1. Add/Multiply/Divide/Subtract
  Old way:
    myNumber = myNumber + 7;
    myNumber = myNumber - 6;
    myNumber = myNumber * 8;
    myNumber = myNumber / 3;
  Better way:
    myNumber+=7;
    myNumber-=6;
    myNumber*=8;
    myNumber/=3

2. Ternary conditionals:
  Old Way:
    if (age >= 16) {
      canHaveLicense = true;
    } else {
      canHaveLicense = false;
    }
  Better Way:
    var canHaveLicense = (age >= 16) ? true : false;

3. Default variable assignments
  Old Way:
    function forestryStuff(arbLimit) {
        var trees;
        if(forest) {
            trees = arbLimit;
        } else {
            trees = 100;
        }
        for(var i = 0; i++; i < = trees) {
            //do something
        }
    }
  Better Way: If arbLimit hasn't been passed into the function, then the value will be 100
    function forestryStuff(arbLimit) {
        var trees = arbLimit || 100;
        for(var i = 0; i < = trees; i++) {
            //do something
        }
    }
4. Associate Array shorthand
  Old Way:
    var myCar = new Array();
      myCar ['Computer'] = 'Sync';
      myCar ['Interior'] = 'Leather';
      myCar ['Tunes'] = 'HDRadio';
      myCar ['Brakes'] = 'ABS';
  Better Way:
    var myCar = {
      'Computer' : 'Sync',
      'Interior' : 'Leather',
      'Tunes' : 'HDRadio',
      'Brakes' : 'ABS'
    };    
    The commas MUST be inlcuded, NO comma after the last entry. IE will fail without it.
*/

  • Facebook
  • Twitter
  • Digg
  • Print
  • email