Created
November 28, 2015 16:29
-
-
Save ElFeesho/90efd61bf585ba5eca11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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