Skip to content

Instantly share code, notes, and snippets.

@ElFeesho
Created November 28, 2015 16:29
Show Gist options
  • Save ElFeesho/90efd61bf585ba5eca11 to your computer and use it in GitHub Desktop.
Save ElFeesho/90efd61bf585ba5eca11 to your computer and use it in GitHub Desktop.
Annoying and difficult to work with
class AwesomeService
{
public List<Model> getMyAwesomeModel()
{
... Takes a long time
}
}
More flexible to work with
class AwesomerService
{
public interface Callback
{
void dataModelAvailable(List<Model> model);
}
public void getModel(Callback callback)
{
new Thread(new Runnable(){
public void run()
{
... Happens off of main thread
new Handler(Looper.getMainLooper()).post(new Runnable(){
public void run()
{
// Thanks to handler, will be run on main thread!
callback.dataModelAvailable();
}
});
}
}).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment